How to link my css, js and image file link in django


Tags: python,css,django

Problem :

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



Solution :

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


    CSS Howto..

    How can I make my numbered scale responsive to scrolling and clicking?

    How do I display epub format book in pure HTML5/CSS/Jquery

    How can I exclude certain elements from css property that has been applied to their parent without using the style attribute

    CSS - How to make rectangle with pointed sides? [duplicate]

    How do i make my Jittery DropDown Menu stable?

    How to get Angular Material Design's directive to scale to image properly in IE?

    How to set color of the Button with jQuery UI?

    How to write syntax in internal css to external css?

    How to fit a div container to cover all the remaining space in between?

    How do i cover the image on navbar as well?

    How do I add a horizontal scrollbar to a table in a div?

    how to print 60 degree rotated watermark in HTML CSS

    How to put a border on a triangle shape using css?

    CSS How to change 3 line (menu icon) color when hover

    How to get a background image to work in CSS and HTML?

    How to access an object from a webpage in selenium

    How to center FontAwesome icon in css before content vertically

    How to position 3 images in two equal height columns?

    How to improve this code to combine/compress CSS using PHP [closed]

    How To Move A DIV Element A Little Towards The Top

    How to change a Pseudo-class style through JavaScript?

    how to spread small image file in background and it should scroll with other elements

    how to revert to original css values after .css() method

    How to change the css class name dynamically in angular 2

    How to construct CSS selector targeting attribute with value containing an ASCII line feed?

    How to center vertically child elements inside div [duplicate]

    Understanding how CSS clear:both is supposed to work

    How to animate selection / deselection in ace editor?

    How to change active color of bootstrap button group to have different color for each button

    How to add lines to
  • elemtents