前几天开始学习Django,偶然看到《Tango with django》这本书,开始关注。但是我被困在这里..模式匹配可能是一个愚蠢的错误..当我单击一个类别时,应显示相关的类别页面快照 但显示以下错误:错误图片
/urls.py
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import url,include
from rango import views
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^rango/', include('rango.urls')),
]
兰戈/urls.py
from django.conf.urls import url
from rango 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'),
]
视图.py
from django.http import HttpResponse
from django.template import RequestContext
from django.shortcuts import render_to_response
from rango.models import Category
from rango.models import Page
from rango.forms import CategoryForm
from django.shortcuts import render
def show_category(request, category_name_slug):
context_dict = {}
try:
category = Category.objects.get(slug=category_name_slug)
pages = Page.objects.filter(category=category)
context_dict['pages'] = pages
context_dict['category'] = category
except Category.DoesNotExist:
context_dict['category'] = None
context_dict['pages'] = None
return render(request, 'rango/category.html', context_dict)
索引视图
def index(request):
context = RequestContext(request)
category_list = Category.objects.order_by('-likes')[:5]
page_list = Page.objects.all()
context_dict = {'categories':category_list, 'pages': page_list}
for category in category_list:
category.url = category.name.replace(' ', '_')
return render_to_response('rango/index.html', context_dict, context)
模型.py
from django.db import models
from django.template.defaultfilters import slugify
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
views = models.IntegerField(default=0)
likes = models.IntegerField(default=0)
slug = models.SlugField(unique=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Category, self).save(*args, **kwargs)
class Meta:
verbose_name_plural = 'Categories'
def __unicode__(self):
return self.name
def __str__(self):
return self.name
class Page(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length=128)
url = models.URLField()
views = models.IntegerField(default=0)
def __unicode__(self):
return self.title
def __str__(self):
return self.title
类别.html
<!DOCTYPE html>
<html>
<head>
<title>Rango</title>
</head>
<body>
<div>
{% if category %}
<h1>{{ category.name }}</h1>
{% if pages %}
<ul>
{% for page in pages %}
<li><a href="{{ page.url }}">{{ page.title }}</a></li>
{% endfor %}
</ul>
<strong>Would you like to add more </strong>
<a href="{% url 'add_page' category.slug %}">pages</a>
<strong>?</strong>
{% else %}
<strong>No pages currently in category.</strong>
{% endif %}
{% else %}
The specified category does not exist!
{% endif %}
</div>
</body>
</html>
索引.html
<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
<title>Rango</title>
</head>
<body>
<h1>Rango says...hello world!</h1>
<h2>Most viewed Categories!</h2>
{% if categories %}
<ul>
{% for category in categories %}
<li><a href="/rango/category/{{ category.slug }}">{{ category.name }}</a></li>
{% endfor %}
</ul>
{% else %}
<strong>There are no categories present.</strong>
{% endif %}
<a href="/rango/add_category/">Add a New Category</a>
<h2>Most Viewed Pages!</h2>
{% if pages %}
<ul>
{% for page in pages %}
<li><a href="/rango/category/{{ category.url }}/{{ page.url }}">{{ page.title }}</a> </li>
{% endfor %}
</ul>
{% else %}
<strong>There are no pages present!</strong>
{% endif %}
<a href="/rango/about/">About</a><br />
<img src="{% static 'rango.jpg' %}" alt="Picture of Rango" />
</body>
</html>