Django - NoReverseMatch 在初始化表单时在创建视图时找不到参数
Django - NoReverseMatch no arguments not found at create view when initializing form
我正在尝试使用一些作为参数传递的数据来初始化表单。我已经在模型中设置了 get_absolute_url 但我不相信错误是这样发生的,这里是回溯:
Traceback (most recent call last):
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\core\handlers\base.py", line 145, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\core\handlers\base.py", line 143, in _get_response
response = response.render()
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\response.py", line 105, in render
self.content = self.rendered_content
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\response.py", line 83, in rendered_content
return template.render(context, self._request)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\base.py", line 171, in render
return self._render(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\base.py", line 936, in render
bit = node.render_annotated(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\base.py", line 903, in render_annotated
return self.render(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\base.py", line 936, in render
bit = node.render_annotated(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\base.py", line 903, in render_annotated
return self.render(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\base.py", line 936, in render
bit = node.render_annotated(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\base.py", line 903, in render_annotated
return self.render(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\defaulttags.py", line 443, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\urls\base.py", line 87, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\urls\resolvers.py", line 677, in _reverse_with_prefix
raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /payments/addtoexistingweek/1/2020-W07
Exception Value: Reverse for 'pagoaexistente' with no arguments not found. 1 pattern(s) tried: ['payments/addtoexistingweek/(?P<carro>[0-9]+)/(?P<semana>[-a-zA-Z0-9_]+)$']
这是我获取参数的方式:
<a href={% url 'pagoaexistente' request.resolver_match.kwargs.carro request.resolver_match.kwargs.semana %}><button type="button" class="btn btn-primary" >
urls.py
path('payments/addtoexistingweek/<int:carro>/<slug:semana>',
views.AgregarPagoSemana.as_view(), name='pagoaexistente'),
views.py
class AgregarPagoSemana(CreateView):
template_name = "AC/add_paymentexistingweek.html"
model = Pagos
form_class = AgregarPagoTransaccionExistente
def get_form_kwargs(self):
kwargs = super(AgregarPagoSemana, self).get_form_kwargs()
kwargs['carro'] = self.kwargs.get('carro')
kwargs['semana'] = self.kwargs.get('semana')
return kwargs
forms.py
class AgregarPagoTransaccionExistente(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.carro = kwargs.pop('carro')
self.semana = kwargs.pop('semana')
super(AgregarPagoTransaccionExistente, self).__init__(*args, **kwargs)
class Meta:
model = Pagos
fields = ('carro', 'pago', 'fecha', 'semana', 'renta')
widgets = {'fecha': forms.DateInput(attrs={'type': 'date'}),
'semana': forms.DateInput(attrs={'type': 'week'})
}
如果没有发送参数,我不明白为什么我会看到:
NoReverseMatch at /payments/addtoexistingweek/1/2020-W07
我在那里看到了 2 个参数
更新
<a href={% url 'pagoaexistente' carro=carro semana=semana %}><button type="button" class="btn btn-primary" >
views.py
class AgregarPagoSemana(CreateView):
template_name = "AC/add_paymentexistingweek.html"
model = Pagos
form_class = AgregarPagoTransaccionExistente
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'semana': self.kwargs['semana'],
'carro': self.kwargs['carro'],
})
return context
def get_form_kwargs(self):
kwargs = super(AgregarPagoSemana, self).get_form_kwargs()
kwargs['carro'] = self.kwargs.get('carro')
kwargs['semana'] = self.kwargs.get('semana')
return kwargs
我想添加这段追溯:
NoReverseMatch
<class 'django.urls.exceptions.NoReverseMatch'>
args[]
context [{'True': True, 'False': False, 'None': None}, {}, {}, {'form':
<AgregarPagoTransaccionExistente bound=False, valid=False, fields=
(carro;pago;fecha;semana;renta)>, 'view': <AC.views.AgregarPagoSemana
object at 0x04FBF790>, 'semana': '2020-W07', 'carro': 1}]
current_app ''
kwargs{}
reverse <function reverse at 0x03BA0B70>
self <django.template.defaulttags.URLNode object at 0x04FA0170>
url ''
view_name 'pagoaexistente'
我对更新的假设是获取参数。
<a href={% url 'pagoaexistente' request.resolver_match.kwargs.carro request.resolver_match.kwargs.semana %}><button type="button" class="btn btn-primary" >
这个 url 应该有关键字参数而不是位置参数。所以你可以这样做:
<a href={% url 'pagoaexistente' carro=request.resolver_match.kwargs.carro semana=request.resolver_match.kwargs.semana %}>
但是那真的很难看。我建议做的是在视图的模板上下文中公开 carro
和 semana
。这样你就可以避免 request.resolver_match.kwargs
的烦恼。
class AgregarPagoSemana(CreateView):
...
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'semana': self.kwargs['semana'],
'carro': self.kwargs['carro'],
})
return context
那么你的模板可以使用:
<a href={% url 'pagoaexistente' carro=carro semana=semana %}>
如果你想避免在视图上使用额外的方法,你可以使用始终包含在上下文中的视图实例:
<a href={% url 'pagoaexistente' carro=view.kwargs.carro semana=view.kwargs.semana %}>
我终于找到了解决方案,问题是在表单模板中,取消按钮的命名空间不正确,这就是它找不到反向匹配的问题。
我正在尝试使用一些作为参数传递的数据来初始化表单。我已经在模型中设置了 get_absolute_url 但我不相信错误是这样发生的,这里是回溯:
Traceback (most recent call last):
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\core\handlers\base.py", line 145, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\core\handlers\base.py", line 143, in _get_response
response = response.render()
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\response.py", line 105, in render
self.content = self.rendered_content
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\response.py", line 83, in rendered_content
return template.render(context, self._request)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\base.py", line 171, in render
return self._render(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\base.py", line 936, in render
bit = node.render_annotated(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\base.py", line 903, in render_annotated
return self.render(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\base.py", line 936, in render
bit = node.render_annotated(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\base.py", line 903, in render_annotated
return self.render(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\base.py", line 936, in render
bit = node.render_annotated(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\base.py", line 903, in render_annotated
return self.render(context)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\template\defaulttags.py", line 443, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\urls\base.py", line 87, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:\Users\jorge.lopez\.virtualenvs\Carros-ZVuxKBiy\lib\site-packages\django\urls\resolvers.py", line 677, in _reverse_with_prefix
raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /payments/addtoexistingweek/1/2020-W07
Exception Value: Reverse for 'pagoaexistente' with no arguments not found. 1 pattern(s) tried: ['payments/addtoexistingweek/(?P<carro>[0-9]+)/(?P<semana>[-a-zA-Z0-9_]+)$']
这是我获取参数的方式:
<a href={% url 'pagoaexistente' request.resolver_match.kwargs.carro request.resolver_match.kwargs.semana %}><button type="button" class="btn btn-primary" >
urls.py
path('payments/addtoexistingweek/<int:carro>/<slug:semana>',
views.AgregarPagoSemana.as_view(), name='pagoaexistente'),
views.py
class AgregarPagoSemana(CreateView):
template_name = "AC/add_paymentexistingweek.html"
model = Pagos
form_class = AgregarPagoTransaccionExistente
def get_form_kwargs(self):
kwargs = super(AgregarPagoSemana, self).get_form_kwargs()
kwargs['carro'] = self.kwargs.get('carro')
kwargs['semana'] = self.kwargs.get('semana')
return kwargs
forms.py
class AgregarPagoTransaccionExistente(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.carro = kwargs.pop('carro')
self.semana = kwargs.pop('semana')
super(AgregarPagoTransaccionExistente, self).__init__(*args, **kwargs)
class Meta:
model = Pagos
fields = ('carro', 'pago', 'fecha', 'semana', 'renta')
widgets = {'fecha': forms.DateInput(attrs={'type': 'date'}),
'semana': forms.DateInput(attrs={'type': 'week'})
}
如果没有发送参数,我不明白为什么我会看到:
NoReverseMatch at /payments/addtoexistingweek/1/2020-W07
我在那里看到了 2 个参数
更新
<a href={% url 'pagoaexistente' carro=carro semana=semana %}><button type="button" class="btn btn-primary" >
views.py
class AgregarPagoSemana(CreateView):
template_name = "AC/add_paymentexistingweek.html"
model = Pagos
form_class = AgregarPagoTransaccionExistente
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'semana': self.kwargs['semana'],
'carro': self.kwargs['carro'],
})
return context
def get_form_kwargs(self):
kwargs = super(AgregarPagoSemana, self).get_form_kwargs()
kwargs['carro'] = self.kwargs.get('carro')
kwargs['semana'] = self.kwargs.get('semana')
return kwargs
我想添加这段追溯:
NoReverseMatch
<class 'django.urls.exceptions.NoReverseMatch'>
args[]
context [{'True': True, 'False': False, 'None': None}, {}, {}, {'form':
<AgregarPagoTransaccionExistente bound=False, valid=False, fields=
(carro;pago;fecha;semana;renta)>, 'view': <AC.views.AgregarPagoSemana
object at 0x04FBF790>, 'semana': '2020-W07', 'carro': 1}]
current_app ''
kwargs{}
reverse <function reverse at 0x03BA0B70>
self <django.template.defaulttags.URLNode object at 0x04FA0170>
url ''
view_name 'pagoaexistente'
我对更新的假设是获取参数。
<a href={% url 'pagoaexistente' request.resolver_match.kwargs.carro request.resolver_match.kwargs.semana %}><button type="button" class="btn btn-primary" >
这个 url 应该有关键字参数而不是位置参数。所以你可以这样做:
<a href={% url 'pagoaexistente' carro=request.resolver_match.kwargs.carro semana=request.resolver_match.kwargs.semana %}>
但是那真的很难看。我建议做的是在视图的模板上下文中公开 carro
和 semana
。这样你就可以避免 request.resolver_match.kwargs
的烦恼。
class AgregarPagoSemana(CreateView):
...
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'semana': self.kwargs['semana'],
'carro': self.kwargs['carro'],
})
return context
那么你的模板可以使用:
<a href={% url 'pagoaexistente' carro=carro semana=semana %}>
如果你想避免在视图上使用额外的方法,你可以使用始终包含在上下文中的视图实例:
<a href={% url 'pagoaexistente' carro=view.kwargs.carro semana=view.kwargs.semana %}>
我终于找到了解决方案,问题是在表单模板中,取消按钮的命名空间不正确,这就是它找不到反向匹配的问题。