/ polls / top / NoReverseMatch为什么会发生这样的错误?

时间:2018-03-25 11:34:43

标签: python django

我收到错误,NoReverseMatch在/ polls / top /反向'详细',参数'(1,)'找不到。尝试了1种模式:['polls/top/<int:pk>/']。 我在views.py中写道

from django.shortcuts import render

from .models import Polls
def top(request):
    data = Polls.objects.order_by('-created_at')
    return render(request,'index.html',{'data':data})

def detail(request):
    data = Polls.objects.order_by('-created_at')
    return render(request,'detail.html',{'data':data})

在子应用的urls.py

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

app_name = 'app'
urlpatterns=[
    url('top/', views.top, name='top'),
    url('detail/<int:pk>/', views.top,name='detail'),
]

在父应用中的urls.py

from django.contrib import admin
from django.conf.urls import url,include

urlpatterns = [
    url('admin/', admin.site.urls),
    url('polls/', include('polls.urls')),
]
index.html中的

<main>
            {% for item in data %}
                            <h2>{{ item.title }}</h2>
                            <a href="{% url 'polls:detail' item.pk %}">SHOW DETAIL
</a>

            {% endfor %}
</main>

当我访问top方法时,发生错误。我真的无法理解为什么我无法访问item.pk.I重写了pk,但是同样的错误发生了。我的代码出了什么问题?我应该如何解决这个问题?

我的新urls.py是

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

app_name = 'app'

urlpatterns=[
    url('top/', views.top, name='top'),
    url('detail/<int:pk>/', views.detail,name='detail'),
]

我的完整追溯是

Traceback (most recent call last):
  File "/Users/xxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/Users/xxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/xxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/xxx/blog/app/views.py", line 7, in top
    return render(request,'index.html',{'data':data})
  File "/Users/xxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/shortcuts.py", line 36, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/Users/xxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/Users/xxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/Users/xxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/template/base.py", line 175, in render
    return self._render(context)
  File "/Users/xxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/template/base.py", line 167, in _render
    return self.nodelist.render(context)
  File "/Users/xxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/template/base.py", line 943, in render
    bit = node.render_annotated(context)
  File "/Users/xxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
    return self.render(context)
  File "/Users/xxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/template/defaulttags.py", line 211, in render
    nodelist.append(node.render_annotated(context))
  File "/Users/xxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
    return self.render(context)
  File "/Users/xxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/template/defaulttags.py", line 447, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "/Users/xxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/urls/base.py", line 88, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "/Users/xxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/urls/resolvers.py", line 632, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'detail' with arguments '(1,)' not found. 1 pattern(s) tried: [‘polls/detail/<int:pk>/']

3 个答案:

答案 0 :(得分:0)

您需要将pk添加到详细视图参数

from django.shortcuts import get_object_or_404

def detail(request, pk):
    poll = get_object_or_404(Polls, pk=pk)
    return render(request,'detail.html',{
        'poll':poll
    })

并且在你的urls.py上你有views.top它应该是views.detail

urlpatterns=[
    path('top/', views.top, name='top'),
    path('detail/<int:pk>/', views.detail, name='detail'),
]

答案 1 :(得分:0)

您在指定网址格式的旧方法和新方法之间感到困惑。您使用的是新方式,因此您需要使用path代替url

path('detail/<int:pk>/', views.detail, name='detail'),

另请注意,您有top而不是detail作为视图;此外,您的detail函数必须接受pk参数。

答案 2 :(得分:-1)

如果您希望视图接受某种参数(您的网址指示您要执行此操作),则必须为视图设置参数。

更改

def top(request):

def top(request, pk = None):