当我收到这个错误时,我明白我没有学习 URL -HTML-views-model 关系。首先,让我展示我的代码。
这是我的views.py:
def category_detail(request, category_name):
links = Link.objects.filter(category__name=category_name)
return render_to_response("category_detail.html", {"links":links}, context_instance=RequestContext(request))
这是models.py:
class Category(models.Model):
name = models.CharField(_("Category"), max_length=255)
user = models.ManyToManyField(User)
def __unicode__(self):
return "%s %s" %(self.user, self.name)
def admin_names(self):
return ', '.join([a.username for a in self.user.all()])
admin_names.short_description = "User Names"
def get_absolute_url(self):
return "/category/%s" % self.name
class Link(models.Model):
user = models.ForeignKey(User)
posted_at = models.DateTimeField(auto_now_add=True)
url = models.URLField()
title = models.CharField(max_length=255, null=True, blank=True)
category = models.ForeignKey(Category)
def __unicode__(self):
return "%s %s %s" %(self.url, self.title, self.category)
这是 HTML 页面:
<div id="profilemenu">
index<p>
{% for category in categories %}
<p><a href="{% url 'category_detail' category.name %}">{{category.name }}</a>
{% endfor %}
<p>
</div>
和 urls.py:
url(r'^category/(?P<category_name>.*)', 'link.views.category_detail', name="category_detail"),
当我单击类别名称打开 category_detail.html 时,浏览器中的 URL 如下:
http://127.0.0.1:8000/category`/
我无法获得类别名称。请你能告诉我我的愚蠢错误吗?:\ 感谢您的时间。