我正在尝试在cookeicutter上建立一个网站,我创建了一个名为“bots”的新应用程序,并在模型中添加了一个名为Trade and 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
Models.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',
)
bots app: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: []
答案 0 :(得分:0)
反向交易细节&#39;参数&#39;(&#39; 1&#39;,)&#39;
您将参数传递为str而不是整数。根据机器人的urls.py,你必须在整数而不是字符串中传递id,所以从交易模型的<{1}}方法中移除str
get_absolute_url