Thursday, September 17, 2020

Static files and linkage in templates

 static files:

--

for css,html,images etc


INSTALLED_APPS = [

...

'django.contrib.staticfiles',

]


STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')


appname(courses)\static\


we can add more folders like


MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')


urls.py

--

from django.conf import settings

from django.conf.urls.static import static

urlpatterns=[

...

]+static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)

urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)



python manage.py collectstatic

#copies all static files to project_root\static folder


in the template html files refer static files..

{% load static %} -- top after html tag

<img src="{% static 'logo.jpg' %}" height=300></img>

<img src="{{course.image.url}}">


No comments:

Post a Comment