1

我创建了一个图书应用程序 - 每本书包含几个部分,每个部分包含几个子部分。

该应用程序运行良好,每个子部分在正确的页面上“显示”其内容等。问题是我希望每个子部分都有不同的 html/css/js 代码和效果。

这是一些 URLS.PY 代码:

 url(r'^admin/', include(admin.site.urls)),
 (r'^$', direct_to_template, {'template': 'index.html'}),
 (r'^book/$','book.views.BookAll'),
 (r'^book/$','book.views.BookAll'),

 (r'^book/(?P<slug>[-\w]+)/$','book.views.Ssection_specific'),
 (r'^book/info/(?P<slug>[-\w]+)/$','book.views.Ssection_details'),

)

一些views.py代码:

def Ssection_specific(request, slug): #display according to specified section object.
    try:
            section = Section.objects.get(slug=slug)
    except Section.DoesNotExist:
            raise Http404        
    ssection = SSection.objects.filter(section = section).order_by('subject')
context = {'ssection' : ssection,'section' : section}
    return render_to_response('section_display.html', context, context_instance = RequestContext(request))# creates

def Ssection_details(request, slug):
    try:
            ssection = SSection.objects.get(slug = slug)
    except SSection.DoesNotExist:
            raise Http404
    context = {'ssection' : ssection}
    return render_to_response('info/ssection_disp.html', context, context_in)

如您所见,每个子部分的页面由 slug 确定。当然,他们每个人都有相同的模板,这是我的问题。我想为每个页面制作不同的 css/js 效果。

4

1 回答 1

2

创建 base.html

base.html

<html>
   <head>
       <title>{% block title %}{% endblock %}</title>

       {% block css %}{% endblock %}
       {% block js %}{% endblock %}
   </head>

   <body>
        {% block content %}{% endblock %}
   </body>
</html>

page1.html

{% extends "base.html" %}

{% block title %}{{block.super}}"title here"{% endblock %}

{% block css %}{{block.super}}
     "css here"
{% endblock %}

{% block js %}{{block.super}}
     "js here"
{% endblock %}

{% block content %}
     content here
{% endblock %}

在其他页面中遵循此模式,您现在可以根据需要使用不同的 css/js

于 2013-02-05T11:36:33.420 回答