2

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 setting.py page

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

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 page located within template folder

Here is my base html template

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

6
  • please try formatting the code first , then only we can answer your queries.. Commented Apr 5, 2016 at 5:26
  • Please see again , I have formatted my code Commented Apr 5, 2016 at 5:43
  • what the error shown on browser console? Commented Apr 5, 2016 at 5:49
  • It would have been better if you had posted the code itself instead of its images. Commented Apr 5, 2016 at 5:50
  • Please see again I have posted the necessary code, Thanks Commented Apr 5, 2016 at 6:02

2 Answers 2

10
    {% 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/

Sign up to request clarification or add additional context in comments.

1 Comment

I had the same problem before, after I put the css file into the static folder then it worked.
2

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' %}">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.