django.urls.exceptions.NoReverseMatch 基于类的列表视图
Posted
技术标签:
【中文标题】django.urls.exceptions.NoReverseMatch 基于类的列表视图【英文标题】:django.urls.exceptions.NoReverseMatch Class Based List Views 【发布时间】:2017-10-14 00:46:12 【问题描述】:我正在尝试在 cookeicutter 上建立一个网站,我创建了一个名为“bots”的新应用程序,并在模型中添加了一个名为 Trade 和 Unit 的类。我在views.py 中创建了两个基于类的视图;详细信息和列表视图。交易详情视图工作正常并指向正确的交易,但是当我尝试访问引用交易列表视图的 html 页面时,该页面返回以下错误。
django.urls.exceptions.NoReverseMatch
django.urls.exceptions.NoReverseMatch: Reverse for 'trade-detail' with
arguments '('1',)' and keyword arguments '' not found. 0 pattern(s)
tried: []
我认为 get_absolute_url 有问题,因为当我从 model.py 中删除它时,上面的列表错误消失并且页面呈现,但是链接不起作用。
我的大部分代码都来自本教程:https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Generic_views
模型.py
from django.db import models
from datetime import date
from django.urls import reverse
from django.urls import reverse_lazy
from django.conf import settings
import uuid
class Unit(models.Model):
TRADE_UNIT = (
('ETH', 'Ethereum'),
('BTC', 'Bitcoin'),
('LTC', 'Litecoin'),
('IOT', 'IOTA'),
('OMG', 'OmiseGo'),
('BCH', 'BitcoinCash'),
)
sell = models.CharField(max_length=3, choices=TRADE_UNIT, blank=True, default='ETH', help_text='Currency to Sell')
buy = models.CharField(max_length=3, choices=TRADE_UNIT, blank=True, default='BTC', help_text='Currency to Buy')
def get_absolute_url(self):
"""
Returns the url to access a particular instance.
"""
return reverse('unit-detail', args=[str(self.id)])
def __str__(self):
"""
String for representing the Model object.
"""
return '%s, %s' % (self.sell, self.buy)
class Meta:
ordering = ['sell']
class Trade(models.Model):
title = models.CharField(max_length=200)
unit = models.ForeignKey(Unit, on_delete=models.SET_NULL, blank=True, null=True)
def get_absolute_url(self):
"""
Returns the url to access a particular book instance.
"""
return reverse('trade-detail', args=[str(self.id)])
def __str__(self):
"""
String for representing the Model object.
"""
return self.title
class Meta:
db_table = 'bots_trade'
ordering = ['title']
Views.py
from django.shortcuts import render
import datetime
from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic
from .models import Trade, Unit, TradeInstance
import uuid
# Create your views here
class TradeDetailView(generic.DetailView):
model = Trade
redirect_field_name = 'redirect_to'
class TradeListView(generic.ListView):
model = Trade
redirect_field_name = 'redirect_to'
class UnitDetailView(generic.DetailView):
model = Unit
redirect_field_name = 'redirect_to'
class UnitListView(generic.ListView):
model = Unit
redirect_field_name = 'redirect_to'
def index(request):
"""
View function for home page of site.
"""
return render(
request,
'bots/index.html',
)
机器人应用程序:urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^trades/$', views.TradeListView.as_view(), name='trades'),
url(r'^trade/(?P<pk>\d+)$', views.TradeDetailView.as_view(), name='trade-detail'),
url(r'^units/$', views.UnitListView.as_view(), name='unit'),
url(r'^unit/(?P<pk>\d+)$', views.UnitDetailView.as_view(), name='unit-detail'),
]
Tracecback EDIT2
这是贸易模型代码:
def get_absolute_url(self):
"""
Returns the url to access a particular book instance.
"""
return reverse('trade-detail', args=(self.id,))
/中断
django.urls.exceptions.NoReverseMatch
django.urls.exceptions.NoReverseMatch: Reverse for 'trade-detail' with arguments '(1,)' and keyword arguments '' not found. 0 pattern(s) tried: []
Traceback (most recent call last)
File "/usr/local/lib/python3.5/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
return self.application(environ, start_response)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 170, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/base.py", line 124, in get_response
response = self._middleware_chain(request)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 44, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 94, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 136, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.5/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.5/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 42, in inner
response = get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/utils/deprecation.py", line 136, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 44, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 94, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 136, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.5/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.5/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 42, in inner
response = get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/utils/deprecation.py", line 136, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 44, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 94, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 136, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.5/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.5/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 42, in inner
response = get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/utils/deprecation.py", line 136, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 44, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 94, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 136, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.5/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.5/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 42, in inner
response = get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/utils/deprecation.py", line 136, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 44, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 94, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 136, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.5/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.5/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 42, in inner
response = get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/utils/deprecation.py", line 136, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 44, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 94, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 136, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.5/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.5/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 42, in inner
response = get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/utils/deprecation.py", line 136, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 44, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 94, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 136, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.5/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.5/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 42, in inner
response = get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/utils/deprecation.py", line 136, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 44, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 94, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 136, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.5/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.5/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 42, in inner
response = get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/utils/deprecation.py", line 136, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 44, in inner
response = response_for_exception(request, exc)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 94, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 136, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python3.5/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.5/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 42, in inner
response = get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/base.py", line 217, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/base.py", line 215, in _get_response
response = response.render()
File "/usr/local/lib/python3.5/site-packages/django/template/response.py", line 109, in render
self.content = self.rendered_content
File "/usr/local/lib/python3.5/site-packages/django/template/response.py", line 86, in rendered_content
content = template.render(context, self._request)
File "/usr/local/lib/python3.5/site-packages/django/template/backends/django.py", line 66, in render
return self.template.render(context)
File "/usr/local/lib/python3.5/site-packages/django/template/base.py", line 208, in render
return self._render(context)
File "/usr/local/lib/python3.5/site-packages/django/test/utils.py", line 94, in instrumented_test_render
return self.nodelist.render(context)
File "/usr/local/lib/python3.5/site-packages/django/template/base.py", line 994, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python3.5/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.5/site-packages/django/template/loader_tags.py", line 174, in render
return compiled_parent._render(context)
File "/usr/local/lib/python3.5/site-packages/django/test/utils.py", line 94, in instrumented_test_render
return self.nodelist.render(context)
File "/usr/local/lib/python3.5/site-packages/django/template/base.py", line 994, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python3.5/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.5/site-packages/django/template/defaulttags.py", line 315, in render
return nodelist.render(context)
File "/usr/local/lib/python3.5/site-packages/django/template/base.py", line 994, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python3.5/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.5/site-packages/django/template/defaulttags.py", line 209, in render
nodelist.append(node.render_annotated(context))
File "/usr/local/lib/python3.5/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.5/site-packages/django/template/base.py", line 1044, in render
output = self.filter_expression.resolve(context)
File "/usr/local/lib/python3.5/site-packages/django/template/base.py", line 711, in resolve
obj = self.var.resolve(context)
File "/usr/local/lib/python3.5/site-packages/django/template/base.py", line 852, in resolve
value = self._resolve_lookup(context)
File "/usr/local/lib/python3.5/site-packages/django/template/base.py", line 915, in _resolve_lookup
current = current()
File "/app/vicki/bots/models.py", line 50, in get_absolute_url
return reverse('trade-detail', args=(self.id,))
File "/usr/local/lib/python3.5/site-packages/django/urls/base.py", line 91, in reverse
return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/usr/local/lib/python3.5/site-packages/django/urls/resolvers.py", line 392, in _reverse_with_prefix
(lookup_view_s, args, kwargs, len(patterns), patterns)
django.urls.exceptions.NoReverseMatch: Reverse for 'trade-detail' with arguments '(1,)' and keyword arguments '' not found. 0 pattern(s) tried: []
【问题讨论】:
你在使用命名空间的 url 吗? 【参考方案1】:使用参数 '('1',)' 反转 'trade-detail'
您将参数作为 str 而不是整数传递。根据机器人的 urls.py,您必须以整数而不是字符串形式传递 id,因此请从 Trade 模型的 get_absolute_url
方法中删除 str
def get_absolute_url(self):
"""
Returns the url to access a particular book instance.
"""
return reverse('trade-detail', args=(self.id,))
【讨论】:
嗨 NeErAj,在根据您的评论编辑我的代码后,我现在收到 builtins.TypeError TypeError: _reverse_with_prefix() 参数后 * 必须是可迭代的,而不是 int 好,底部添加了 在元组 args=(self.id,) 中使用逗号,就像在 id 之后一样 我更新了回溯,它现在返回之前的错误:django.urls.exceptions.NoReverseMatch django.urls.exceptions.NoReverseMatch: Reverse for 'trade-detail' with arguments '(1,)'并且找不到关键字参数“”。尝试了 0 个模式:[] 感谢@NeErAjKuMaR 提供this comment。帮助调试this error以上是关于django.urls.exceptions.NoReverseMatch 基于类的列表视图的主要内容,如果未能解决你的问题,请参考以下文章