代码之家  ›  专栏  ›  技术社区  ›  Rashed Hasan Vijayaragavan

在django中找不到带关键字参数{'slug':'hello world'}'的'detail'的反向

  •  1
  • Rashed Hasan Vijayaragavan  · 技术社区  · 6 年前

    我正在尝试使用django构建一个博客应用程序。现在我要展示 article 详细信息使用 slug . 当我用贝娄 RegEx 用作细节的子弹 url 我发现了这个错误。有人能帮我解决这个问题吗?

    articles -应用程序 urls.py IS

    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 IS

    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 IS

    {% 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-]+)/$']
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   user3850    6 年前

    很明显是你的 urls.py 被打破:

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

    可能是最主要的,包括 articles/urls.py . 包含模式以 $ . $ 意思是“url的结尾”。您的regex不能匹配任何url。