Answer the question
In order to leave comments, you need to log in
How to include css styles in Django?
Hello.
Tell me please, I've been suffering for the second day, I read the docks, googled, but there's little sense.
I installed Django, created a project, everything is fine, but as soon as I decided to connect css, then ...
I have windows 7, Django 1.4, MySQL
Here is what I added to the code:
#setting.py<br/>
from os.path import join, abspath, normpath, dirname<br/>
<br/>
ProjectDir = dirname(abspath(__file__))<br/>
<br/>
def tpl_dir(src):<br/>
return normpath(join(ProjectDir, src)).replace('\\', '/') <br/>
<br/>
MEDIA_ROOT = tpl_dir('media')<br/>
MEDIA_URL = '/media/'<br/>
<br/>
STATIC_ROOT = tpl_dir('static')<br/>
STATIC_URL = '/static/'<br/>
<br/>
ADMIN_MEDIA_PREFIX = '/static/admin/'<br/>
<br/>
STATICFILES_DIRS = (tpl_dir('static'),)<br/>
<br/>
TEMPLATE_DIRS = (tpl_dir('templates'),)<br/>
#urls.py<br/>
from django.conf import settings<br/>
<br/>
if settings.DEBUG:<br/>
urlpatterns += patterns('',(r'^robots.txt$', 'django.views.static.serve',<br/>
{'document_root': settings.MEDIA_ROOT, 'path': "robots.txt"}),<br/>
(r'^favicon.ico$', 'django.views.static.serve',<br/>
{'document_root': settings.MEDIA_ROOT, 'path': "favicon.ico"}),<br/>
(r'^media/(?P.*)$', 'django.views.static.serve',<br/>
{'document_root': settings.MEDIA_ROOT}),<br/>
)<br/>
<br/>
<code>#base.html<br/>
<br/>
link rel="stylesheet" href="{{ STATIC_URL }}css/style.css" type="text/css" media="all" /<br/>
</code><br/>
Но ничего не происходит. Надпись должна стать красной.<br/>
<br/>
Подскажите пожалуйста, что я делаю не так.<br/>
Спасибо
Answer the question
In order to leave comments, you need to log in
The problem is the wrong setting of STATIC_ROOT and STATICFILES_DIRS.
STATIC_ROOT is a temporary folder where statics are collected in production with the ./manage.py collectstatic command. During development, it can be empty. I usually call it "collected_static" and make a folder somewhere
files user_uploads <- indicates MEDIA_ROOT here collected_static <- STATIC_ROOT points here
Starting from version 1.3, it is used by default in Dzhang django.contrib.staticfiles
- an excellent application that simplifies working with project statics.
In development mode, that is, when you runserver
run it, this application catches all requests that start with STATIC_URL
and looks for the requested file without this prefix in each application's static folder in INSTALLED_APPS
.
Without going into details, which are perfectly described in the manual, I can advise you this: make an application with a name, for example, sitemedia, connect it to INSTALLED_APPS, create a static directory inside this application and leave all the static there.
Pay attention only: statics, not media! These are different directories for different purposes.
And one more thing: ADMIN_MEDIA_PREFIX is not used in 1.4.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question