Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: how do you serve media / stylesheets and link to them within templates

Variations of this question have been asked, but I'm still unable to get my stylesheets to load correctly when my templates are rendered.

I'm attempting to serve static media from the Django process during development - which is strongly discouraged in production, I'm aware. I'll post my configuration and my template, and hopefully someone can help me to understand where I'm going wrong.

Note that I did try to follow the example on the Django project website, however it doesn't mention how to refer to your stylesheets from a template. I've also tried many different variations of the same thing, so my code/settings may be a little off from what's described.

settings.py

MEDIA_ROOT = 'D:/Dev Tools/django_projects/dso/media' MEDIA_URL = '/media/' ADMIN_MEDIA_PREFIX = '/media/' 

urls.py

from django.conf.urls.defaults import * from django.conf import settings from django.contrib import admin  admin.autodiscover()  urlpatterns = patterns('',     (r'^admin/(.*)', admin.site.root),     (r'^ovramt/$', 'dso.ovramt.views.index'), )  if settings.DEBUG:     urlpatterns += patterns('',         (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),     ) 

Within my template:

<head>  <title> {% block title %} DSO Template {% endblock %} </title>  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" > <link rel="stylesheet" type="text/css" href="../media/styles.css"> </head> 

I assure you, the files (templates/media) are in the correct directory on my file system. If there's any extra information I need to provide, please post a comment.


Edit:

One of the problems I was having was the use of a '/' prepending my links. If the forward slash is prepended, the link is opened from the root of the site. If there is no forward slash, the link is opened in the current level. An example:

www.example.com/application/ has a link "/app2/ and a link "app3/".
app2 will open at www.example.com/app2/ and app3 will open at www.example.com/application/app3/. This was confusing me I think.

like image 724
Josh Smeaton Avatar asked Jan 15 '09 08:01

Josh Smeaton


People also ask

How do you link stylesheets in Django?

You need to add below line to your settings.py along with load static and static rules plus debug mode true. No need to add anything into urls.py file. Django will automatically find static folder inside each app folder.

How do I serve media files in Django?

To make Django development server serve static we have to add a URL pattern in sitewide urls.py file. Now visit http://127.0.0.1:8000/media/python.png again, this time you should be able to see the image. Just as with static files, in the production, you should always use a real web server to serve media files.

How do I serve a template in Django?

To configure the Django template system, go to the settings.py file and update the DIRS to the path of the templates folder. Generally, the templates folder is created and kept in the sample directory where manage.py lives. This templates folder contains all the templates you will create in different Django Apps.

How can we load css file in our Django templates?

The static folder should be created in your project folder, that's where you can create in static folder a folder called css, and then in the css folder add the css files.


1 Answers

I just had to figure this out myself.

settings.py:

MEDIA_ROOT = 'C:/Server/Projects/project_name/static/' MEDIA_URL = '/static/' ADMIN_MEDIA_PREFIX = '/media/' 

urls.py:

from django.conf import settings ... if settings.DEBUG:     urlpatterns += patterns('',         (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),     ) 

template file:

<link rel="stylesheet" type="text/css" href="/static/css/style.css" /> 

With the file located here:

"C:/Server/Projects/project_name/static/css/style.css" 
like image 107
Ty. Avatar answered Sep 21 '22 20:09

Ty.