【问题标题】:Tango with Django : URLconf defined in tango_with_django_project.urls. Page not foundTango with Django:在 tango_with_django_project.urls 中定义的 URLconf。网页未找到
【发布时间】:2017-11-25 10:37:10
【问题描述】:

前几天开始学习Django,偶然看到《Tango with django》这本书,开始关注。但是我被困在这里..模式匹配可能是一个愚蠢的错误.. 当我点击一个类别时,相关的类别页面应该会显示snapshot 但显示以下错误:Error image

/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')),

]

rango/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'),
]

views.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)

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>

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>

【问题讨论】:

  • 您要查看http://127.0.0.1:8000/rango/category/name 页面吗?
  • 请发布您的错误消息的文本,而不是它的屏幕截图。也试着给一个minimal reproducible example,而不是你的整个代码。
  • 抱歉,我是 StackOverflow 的新手!忘了提一下,我的索引页面显示了前 5 个类别的列表。当我单击一个类别时,我应该看到与该类别对应的页面列表。
  • 您可以发布索引页面的视图吗?
  • 当然..给你

标签: python django tango urlconf


【解决方案1】:

在您的index.html 中更改此行,

<a href="/rango/category/{{ category.slug }}">{{ category.name }}</a></li>

到,

<a href="{% url 'show_category' category_name_slug=category.slug %}">{{ category.name }}</a></li>

【讨论】:

  • 最初的 127.0.0.1:8000/rango 页面现在出现错误。它在 /rango/ Reverse 处显示 NoReverseMatch 用于 'show_category' 且未找到参数 '('',)'。尝试了 1 种模式:['rango/category/(?P[\\w\\-]+)/$']
  • 仍然类似的错误:NoReverseMatch at /rango/ Reverse for 'show_category' with keyword arguments '{'category_name_slug': ''}' not found。尝试了 1 种模式:['rango/category/(?P[\\w\\-]+)/$']
猜你喜欢
  • 2015-09-20
  • 1970-01-01
  • 2016-04-11
  • 2014-10-05
  • 2011-03-29
  • 2013-08-11
  • 2015-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多