反转'详细信息'使用关键字参数' {' slug':' hello-world'}'在Django找不到

时间:2018-06-07 18:18:04

标签: python django

我正在尝试使用django构建一个博客应用。现在,我需要使用article显示slug个详细信息。当我使用下面的RegEx作为详细信息的slug url时,我收到了这个错误。有人会帮我解决这个问题吗?

articles - 应用urls.py是 -

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.article_list, name="list"),
    url(r'^(?P<slug>[\w-]+)/$', views.article_detail, name="detail"),

]

views.py是 -

from django.shortcuts import render
from .models import Article
from django.http import HttpResponse

# Create your views here.
def article_list(request):
    articles = Article.objects.all().order_by('date')
    return render(request, 'articles/article_list.html', {'articles': articles})

def article_detail(request, slug):
    return HttpResponse(slug)

article_list.html是 -

{% extends 'base_layout.html' %}

{% block content %}
<h1>Article List:</h1>
<div class="articles">
  {% for article in articles %}
    <div class="article">
      <h2><a href="{% url 'detail' slug=article.slug %}">{{ article.title }}</a></h2>
      <p>{{ article.snippet }}</p>
      <p>{{ article.date }}</p>
    </div>
  {% endfor %}
</div>
{% endblock %}

回溯 -

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/articles/

Django Version: 2.0.5
Python Version: 3.6.5
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'articles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template C:\Users\rashed\Documents\django- 
blog\djangonautic\templates\base_layout.html, error at line 0
Reverse for 'detail' with keyword arguments '{'slug': 'hello-world'}' not found. 1 pattern(s) tried: ['articles/$(?P<slug>[\\w-]+)/$']
   1 : {% load static from staticfiles %}
   2 : 
   3 : <!DOCTYPE html>
   4 : <html lang="en" dir="ltr">
   5 :   <head>
   6 :     <meta charset="utf-8">
   7 :     <title>Articles</title>
   8 :     <link rel="stylesheet" href="{% static 'styles.css' %}">
   9 :   </head>
   10 :   <body>


Traceback:

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\rashed\Documents\django-blog\djangonautic\articles\views.py" in article_list
  8.     return render(request, 'articles/article_list.html', {'articles': articles})

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\shortcuts.py" in render
  36.     content = loader.render_to_string(template_name, context, request, using=using)

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\loader.py" in render_to_string
  62.     return template.render(context, request)

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\backends\django.py" in render
  61.             return self.template.render(context)

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in render
  175.                     return self._render(context)

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in _render
  167.         return self.nodelist.render(context)

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in _render
  167.         return self.nodelist.render(context)

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\loader_tags.py" in render
  67.                 result = block.nodelist.render(context)

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\defaulttags.py" in render
  211.                     nodelist.append(node.render_annotated(context))

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\defaulttags.py" in render
  447.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\base.py" in reverse
  88.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))

File "C:\Users\rashed\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
  632.         raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /articles/
Exception Value: Reverse for 'detail' with keyword arguments '{'slug': 'hello-world'}' not found. 1 pattern(s) tried: ['articles/$(?P<slug>[\\w-]+)/$']

2 个答案:

答案 0 :(得分:2)

显然你的一个urls.py已经破了:

pattern(s) tried: ['articles/$(?P<slug>[\\w-]+)/$']

可能是主要的一个,其中包含articles/urls.py。包含模式以$结尾。 $表示“网址结束”。您的正则表达式无法匹配任何网址。

答案 1 :(得分:1)

在article_list中,从

更改
<h2><a href="{% url 'detail' slug=article.slug %}">{{ article.title }}</a></h2>

<h2><a href="{{ article.slug }}"><li>{{ article.title }}</li></a></h2>