Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python NoReverseMatch at/polls/top/为什么会发生这样的错误?_Python_Django - Fatal编程技术网

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

Python NoReverseMatch at/polls/top/为什么会发生这样的错误?,python,django,Python,Django,我遇到一个错误,找不到参数为“(1,)”的“详细信息”的NoReverseMatch at/polls/top/Reverse。尝试了1个模式:['polls/top/']。 我在views.py上写的 from django.shortcuts import render from .models import Polls def top(request): data = Polls.objects.order_by('-created_at') return render(

我遇到一个错误,找不到参数为“(1,)”的“详细信息”的NoReverseMatch at/polls/top/Reverse。尝试了1个模式:[
'polls/top/'
]。 我在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})
在子应用程序的URL.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'),
]
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>

{数据%中的项的%1}
{{item.title}
{%endfor%}
当我访问top方法时,会发生错误。我真的无法理解为什么我不能访问item.pk。我重写了pk,但同样的错误也会发生。我的代码中有什么错误?我应该如何修复

我的新URL.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'),
]
从django.conf.url导入url
从django.conf导入设置
从django.conf.url.static导入静态
从…起导入视图
应用程序名称='应用程序'
URL模式=[
url('top/',views.top,name='top'),
url('detail/',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>/']
回溯(最近一次呼叫最后一次):
文件“/Users/xxx/anaconda3/envs/py36/lib/python3.6/site packages/django/core/handlers/exception.py”,第35行,在内部
响应=获取响应(请求)
文件“/Users/xxx/anaconda3/envs/py36/lib/python3.6/site packages/django/core/handlers/base.py”,第128行,在“获取”响应中
response=self.process\u异常\u由\u中间件(e,请求)
文件“/Users/xxx/anaconda3/envs/py36/lib/python3.6/site packages/django/core/handlers/base.py”,第126行,在“获取”响应中
响应=包装的回调(请求,*回调参数,**回调参数)
文件“/Users/xxx/blog/app/views.py”,顶部第7行
返回呈现(请求,'index.html',{'data':data})
文件“/Users/xxx/anaconda3/envs/py36/lib/python3.6/site packages/django/shortcuts.py”,第36行,在渲染中
content=loader.render_to_string(模板名称、上下文、请求、using=using)
文件“/Users/xxx/anaconda3/envs/py36/lib/python3.6/site packages/django/template/loader.py”,第62行,呈现为字符串
返回template.render(上下文、请求)
文件“/Users/xxx/anaconda3/envs/py36/lib/python3.6/site packages/django/template/backends/django.py”,第61行,呈现
返回self.template.render(上下文)
文件“/Users/xxx/anaconda3/envs/py36/lib/python3.6/site packages/django/template/base.py”,第175行,在渲染中
返回self.\u呈现(上下文)
文件“/Users/xxx/anaconda3/envs/py36/lib/python3.6/site packages/django/template/base.py”,第167行,在_render中
返回self.nodelist.render(上下文)
文件“/Users/xxx/anaconda3/envs/py36/lib/python3.6/site packages/django/template/base.py”,第943行,在render中
位=节点。带注释的渲染(上下文)
文件“/Users/xxx/anaconda3/envs/py36/lib/python3.6/site packages/django/template/base.py”,第910行,在带注释的render_中
返回self.render(上下文)
文件“/Users/xxx/anaconda3/envs/py36/lib/python3.6/site packages/django/template/defaulttags.py”,第211行,在渲染中
nodelist.append(node.render_注释(上下文))
文件“/Users/xxx/anaconda3/envs/py36/lib/python3.6/site packages/django/template/base.py”,第910行,在带注释的render_中
返回self.render(上下文)
文件“/Users/xxx/anaconda3/envs/py36/lib/python3.6/site packages/django/template/defaulttags.py”,第447行,在渲染中
url=reverse(视图名称,args=args,kwargs=kwargs,当前应用程序=当前应用程序)
文件“/Users/xxx/anaconda3/envs/py36/lib/python3.6/site packages/django/url/base.py”,第88行,相反
将iri_返回到_uri(解析器。_使用_前缀反向_(视图,前缀,*args,**kwargs))
文件“/Users/xxx/anaconda3/envs/py36/lib/python3.6/site packages/django/url/resolvers.py”,第632行,带前缀
提升NoReverseMatch(msg)
django.url.exceptions.NoReverseMatch:找不到参数为“(1,)”的“详细信息”的反向。已尝试1个模式:['polls/detail/']

如果希望视图接受某种类型的参数(您的url表明您希望这样做),则必须为视图设置参数

改变

def top(request):


您需要将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
    })
在你的url.py上有views.top,应该是views.detail

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

您混淆了指定URL模式的新旧方法。您正在使用新的方式,因此需要使用
path
而不是
url

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

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

在俯视图和细节视图中,您想要实现什么?他们看起来很混乱。你需要发布完整的追踪。您显示的代码不会出现此错误。@RamkishoreM top view显示所有内容的轮询。当我单击
,程序进入详细视图,选择单击的轮询的模型,并在detail.html中显示模型的详细信息。感谢您的回答。我编写了您的代码,
def top(request,pk=None):data=Polls.objects.order_by('-created_at')return render(request,'index.html',{'data':data})
但也会发生同样的错误。我怀疑这个方法中没有使用pk变量,所以我误解了你的答案吗?谢谢你的回答。我编写了你的代码,但也会发生同样的错误。我应该如何解决?我们还有一个问题需要解决。我已经把它添加到帖子中了,谢谢,但我还是遇到了同样的错误…如果你知道什么,请帮我更新我的问题。如果你知道什么,请帮我将url链接到我的代码片段中的路径?
path('detail/<int:pk>/', views.detail, name='detail'),