python - 探戈与 Django : URLconf defined in tango_with_django_project. 网址。找不到网页

标签 python django tango urlconf

前几天开始学习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')),

]

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

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

索引 View

   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>

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>

最佳答案

更改 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>

关于python - 探戈与 Django : URLconf defined in tango_with_django_project. 网址。找不到网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44694475/

相关文章:

python - Pandas MultiIndex 获取所有具有标签值的行

python - 如何在 odoo 上找到模态视图 ID?

javascript - 单击喜欢按钮时,无法使用 ajax 更改磁带中帖子的喜欢数量

google-project-tango - 如何获取 Tango 文档?

python - 错误 "ImportError: DLL load failed: %1 is not a valid Win32 application"

python - 如何从 .yuv 视频文件中提取某些帧并使用 FFmpeg、OpenCV 和 python 创建新视频?

Python Django : RuntimeError: Model class django. contrib.sites.models.Site(错误继续)

python - 使用 Django 将 raw_post_data 保存到 FileField

java - 无法在 Google Tango 设备中运行 Tango java_basic_examples