Я пытаюсь инициализировать форму с некоторыми данными, передаваемыми в качестве параметров. У меня уже есть в модели 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
формы .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'
Мое предположение с обновлением - получение параметров.