我正在阅读带有Django书的Tango,并遇到了一个我似乎无法解决的错误。在我的Django Web应用程序中,在扩展base.html的模板中,我的标题栏和侧栏块没有渲染,但我的身体块是。我似乎无法弄清楚是什么问题。我需要在项目中实现更多的块吗?
这是我的base.html
<!DOCTYPE html>
{% load staticfiles %}
{% load mango_template_tags %}
<html>
<head>
<title>
Mango -
{% block title_block %}
How to Tango with Django
{% endblock %}
</title>
</head>
<body>
<div>
{% block sidebar_block %}
{% get_category_list category %}
{% endblock %}
</div>
<div>
{% block body_block %}
{% endblock %}
</div>
<br />
<div>
<ul>
{% if user.is_authenticated %}
<li><a href="{% url 'restricted' %}">Restricted Page</a></li>
<li><a href="{% url 'logout' %}">Logout</a></li>
{% else %}
<li><a href="{% url 'register' %}">Sign Up</a></li>
<li><a href="{% url 'login' %}">Login</a></li>
{% endif %}
<li><a href="{% url 'add_category' %}">Add New Category</a></li>
<li><a href="{% url 'about' %}">About</a></li>
<li><a href="{% url 'index' %}">Index</a></li>
</ul>
</div>
</body>
</html>
这是扩展base.html,about.html
的模板之一{% extends 'rango/base.html' %}
{% load staticfiles %}
{% block title_block %}
About
{% endblock %}
{% block body_block %}
<div>
<h1 class="page-header">About Page</h1>
This tutorial has been put together by: leifos and maxwelld90
</div>
<div>
<img src="{% static "images/flower.jpg " %}"
alt = "picture of flower"
width=300 height = 300/> <br />
<img src="{{ MEDIA_URL}}city.jpg"
alt = "picture of city"
width=300 height = 300/>
</div>
{% endblock %}
以防万一需要的是app和项目的urls.py
from django.conf.urls import url, include
from mango import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'about/$', views.about, name='about'),
url(r'^add_category/$', views.add_category, name='add_category'),
url(r'^category/(?P<category_name_slug>[\w\-]+)/$', views.show_category, name='show_category'),
url(r'^category/(?P<category_name_slug>[\w\-]+)/add_page/$', views.add_page, name='add_page'),
url(r'^register/$', views.register, name='register'),
url(r'^login/$', views.user_login, name='login'),
url(r'^restricted/', views.restricted, name="restricted"),
url(r'^logout/$', views.user_logout, name='logout'),
]
from django.conf.urls import url, include
from django.contrib import admin
from mango import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^mango/', include('mango.urls')),
url(r'^admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
如果需要更多文件,我会根据我的问题编辑它们。谢谢你的帮助。
关于视图
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import render
from mango.models import Category
from mango.models import Page
from mango.forms import CategoryForm, PageForm
from mango.forms import UserForm, UserProfileForm
def about(request):
context_dict = {'description': "It has random functionalities",
'author': "Billy Bob"}
return render(request, 'mango/about.html', context=context_dict)