Python Tango with Django:URLconf在Tango_with_Django_project.url中定义。找不到页面
几天前开始学习Django,偶然发现这本书《与Django的探戈》,并开始学习。但我被困在这里了..可能是模式匹配的愚蠢错误。。 单击某个类别时,应显示相关的类别页面 但显示了以下错误: /url.pyPython Tango with Django:URLconf在Tango_with_Django_project.url中定义。找不到页面,python,django,tango,urlconf,Python,Django,Tango,Urlconf,几天前开始学习Django,偶然发现这本书《与Django的探戈》,并开始学习。但我被困在这里了..可能是模式匹配的愚蠢错误。。 单击某个类别时,应显示相关的类别页面 但显示了以下错误: /url.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.con
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')),
]
rango/url.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'),
]
索引视图
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)
models.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
category.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>
兰戈
{%if类别%}
{{category.name}
{%if页面%}
{第%页中的第%页}
{%endfor%}
是否要添加更多内容
?
{%else%}
当前类别中没有页面。
{%endif%}
{%else%}
指定的类别不存在!
{%endif%}
index.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>
{%load staticfiles%}
兰戈
兰戈说…你好,世界!
浏览次数最多的类别!
{%if类别%}
{categories%%中的类别为%s}
{%endfor%}
{%else%}
不存在任何类别。
{%endif%}
浏览次数最多的网页!
{%if页面%}
{第%页中的第%页}
{%endfor%}
{%else%}
没有页面强>
{%endif%}
在index.html
中更改此行
<a href="/rango/category/{{ category.slug }}">{{ category.name }}</a></li>
对,
您是否试图查看http://127.0.0.1:8000/rango/category/name
page?请发布错误消息的文本,而不是屏幕截图。也试着给出一个,而不是你的全部代码。对不起,我是StackOverflow的新手!忘了提一下,我的索引页显示了前5个类别的列表。当我单击一个类别时,我应该会看到与该类别对应的页面列表。你能发布索引页的视图吗?当然。这里你看到了初始页现在给出了错误。它说NoReverseMatch at/rango/Reverse代表“show_category”,带有参数(“”,)“没有找到。尝试了1个模式:[“rango/category/(?P[\\w\\-]+)/$”]仍然存在类似错误:找不到具有关键字参数“{category\u name\u slug':”}”的“show\u category”的/rango/Reverse的NoReverseMatch。尝试了1个模式:['rango/category/(?P[\\w\\\-]+)/$]
<a href="/rango/category/{{ category.slug }}">{{ category.name }}</a></li>
<a href="{% url 'show_category' category_name_slug=category.slug %}">{{ category.name }}</a></li>