I
I
Isaac Clark2012-06-06 15:44:39
css
Isaac Clark, 2012-06-06 15:44:39

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': &quot;robots.txt&quot;}),<br/>
 (r'^favicon.ico$', 'django.views.static.serve',<br/>
 {'document_root': settings.MEDIA_ROOT, 'path': &quot;favicon.ico&quot;}),<br/>
 (r'^media/(?P.*)$', 'django.views.static.serve',<br/>
 {'document_root': settings.MEDIA_ROOT}),<br/>
 )<br/>
<br/>
<code>#base.html<br/>
<br/>
link rel=&quot;stylesheet&quot; href=&quot;{{ STATIC_URL }}css/style.css&quot; type=&quot;text/css&quot; media=&quot;all&quot; /<br/>
</code><br/>
Но ничего не происходит. Надпись должна стать красной.<br/>
<br/>
Подскажите пожалуйста, что я делаю не так.<br/>
Спасибо

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
kmike, 2012-06-06
@Dark_Knight

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

STATICFILES_DIRS is a list of folders where the project's shared static is stored and from which it is collected into STATIC_ROOT by the ./manage.py collectstatic command.
In addition to folders from STATICFILES_DIRS, collectstatic also looks into the static folder by default for each application from INSTALLED_APPS.

M
marazmiki, 2012-06-06
@marazmiki

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 runserverrun it, this application catches all requests that start with STATIC_URLand 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.

S
scream4ik, 2012-06-06
@scream4ik

offhand in urlpatterns there is no STATIC_URL

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question