Reputation: 150
I am a newbie in Django 1.9.5 and using windows as my platform. I have a problem to link my css, images and js to django templage,
Here is my project structure
Here is my settings page
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) PROJECT_DIR = os.path.dirname(__file__) STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'statics'), ) MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/'
Here is my main url.py page
from django.conf import settings from django.conf.urls import include, url from django.conf.urls.static import static from django.contrib import admin from django.contrib.staticfiles.urls import staticfiles_urlpatterns urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^mysite/', include('myapp.urls')), # (r'^media/(?P.*)$', 'django.views.static.serve', # {'document_root': settings.MEDIA_ROOT}), url(r'^media/(?P.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) urlpatterns += staticfiles_urlpatterns()
Here is my base html template
Github link My project in github I have tried all possible combination but failed in 2 days. Any help will be appropriated and I will be grateful to you Thanks
Upvotes: 2
Views: 17185
Reputation: 307
first of all add following code in
urls.py use this library
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
add following code in settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS= [os.path.join(BASE_DIR,'assets'),] # this variable have been created for adding static resourcess
in template(html) file you will first of all load
{% load static %}
<link rel="stylesheet" href=" {% static '/boostrap4.4/bootstrap.min.css' %}">
<link rel="stylesheet" href=" {% static '/fontawesome-free-5.12.1-web/css/all.css' %}">
<link rel="stylesheet" href=" {% static '/css/style.css' %}">
Upvotes: 2
Reputation: 1320
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'pathtostaticfile' %}" />
You can using statcfiles tag to load your static file. With pathtostaticfile is your static file
More detail https://docs.djangoproject.com/en/1.9/intro/tutorial06/
Upvotes: 10