Enabling css in a Django template

Good day to all! I have a project" proj " on Django, it created an application "blog". In the file "..../proj/blog/templates/page.tpl" you need to connect the file "style. css" which is located in the same folder. How to specify the path that would be when calling

render_to_response('page.tpl', context)

Did the generated page find the css file?

Author: Nicolas Chabanovsky, 2011-07-05

2 answers

If you use a built-in server for development, then you need to put the style file in a folder with static (static files). To do this, in urls.py you need to insert

(r'^statika/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': '/path/to/media'}),

Instead of the word statika, you can write anything, and instead of /path/to / media, the full path to the folder where you want to store statics( for example c:/django_proj/media/ for windows). If you want the css files to be placed together with the templates, then you need to specify the path to the folder with the templates. And then in page.tpl you will you need to register

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

Instead of statika, you can use the word you specified in the urls.

Here you can read more about the distribution of statics on the developer's server.

 7
Author: rnd_d, 2011-07-05 08:09:08

In Django 1.3, everything has become much easier. In the template, for example, we connect css or js:

<link href="{{ STATIC_URL }}css/style.css" rel="stylesheet" type="text/css" media="screen" />

Your settings.py:

STATIC_URL = '/personal_site/static/'

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles', # <--- Добавлено
    ...
    )

And a couple more settings. You can read more here - the staticfiles app.

 5
Author: DroidAlex, 2015-04-26 15:51:05