python - django - static files in base template -
how create base of operations template utilize static files , create other templates inherits base of operations template utilize same static files?
as read in django documentation writes how utilize static files used specific app. can't seem find way create utilize static files outside app.
given app inherits everything, static files.
my settings.py:
static_url = '/static/' # additional locations of static files staticfiles_dirs = ( # set strings here, "/home/html/static" or "c:/www/django/static". # utilize forwards slashes, on windows. # don't forget utilize absolute paths, not relative paths. ) # list of finder classes know how find static files in # various locations. staticfiles_finders = ( 'django.contrib.staticfiles.finders.filesystemfinder', 'django.contrib.staticfiles.finders.appdirectoriesfinder', # 'django.contrib.staticfiles.finders.defaultstoragefinder', ) installed_apps = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'south', 'myproject.apps.finance', 'myproject.apps.base', 'django.contrib.admin', ) template_context_processors = ( 'django.core.context_processors.debug', 'django.core.context_processors.i18n', 'django.core.context_processors.media', 'django.core.context_processors.static', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', )
this current homepage settings:
url homepage:
from django.conf.urls import patterns, include, url myproject.views import hello urlpatterns = patterns('', ('', hello), )
view of homepage:
from django.shortcuts import render_to_response django.http import http404, httpresponse import datetime def hello(request): hello = "hello world" homecoming render_to_response('home/home.html', {'hello': hello})
homepage template:
{% extends "base/base.html" %} {% block title %} homepage {% endblock %} {% block content %} <p>i {{ hello }}</p> {% endblock %}
base template:
<!doctype html public "-//w3c//dtd html 4.01//en"> <html lang="en"> <head> <link rel="stylesheet" href="{{ static_url }}/css/style.css"> <title>{% block title %}{% endblock %}</title> </head> <body> <h1>my personal finance site</h1> {% block content %}{% endblock %} {% block footer %} <section class="divider1"> <p>thanks visiting site.</p> <p>all rights reserved</p> </section> {% endblock %} </body> </html>
my css file located in empty project called base. think there improve way utilize static files outside given app.
so best way link base of operations template css file create other templates intherits base, same css file configuration?
base.html
<!doctype html public "-//w3c//dtd html 4.01//en"> <html lang="en"> <head> {% block css %} <link rel="stylesheet" href="{{ static_url }}/css/style.css"> {% endblock %} <title>{% block title %}{% endblock %}</title> </head> <body> <h1>my personal finance site</h1> {% block content %}{% endblock %} {% block footer %} <section class="divider1"> <p>thanks visiting site.</p> <p>all rights reserved</p> </section> {% endblock %} </body> </html>
page.html
{% extends "base/base.html" %} {% block title %} homepage {% endblock %} {% block css %}{{block.super}} //put css here {% endblock %} {% block content %} <p>i {{ hello }}</p> {% endblock %}
urls.py
from django.conf.urls import patterns, include, url django.conf.urls.static import static django.contrib import admin django.contrib.staticfiles.urls import staticfiles_urlpatterns project_name import settings admin.autodiscover() urlpatterns = patterns('', ....... ) + static(settings.media_url, document_root=settings.media_root) urlpatterns += staticfiles_urlpatterns()
settings.py
import os import sys .......................... site_root = os.path.dirname(__file__) sys.path.insert(0, os.path.join(site_root, 'app_name')) media_root = os.path.join(site_root, 'media') media_url = '/media/' static_root = os.path.join(site_root, 'static') static_url = '/static/' staticfiles_dirs = ( # set strings here, "/home/html/static" or "c:/www/django/static". # utilize forwards slashes, on windows. # don't forget utilize absolute paths, not relative paths. os.path.join(site_root, 'staticfiles'), ) template_dirs = ( # set strings here, "/home/html/django_templates" or "c:/www/django/templates". # utilize forwards slashes, on windows. # don't forget utilize absolute paths, not relative paths. os.path.join(site_root, 'templates'), ) ....................
python html css django
No comments:
Post a Comment