Answer the question
In order to leave comments, you need to log in
NoReverseMatch path building/argument passing problem?
Models.py :
class Debtor(models.Model):
...
slug = models.SlugField(max_length=150, verbose_name='Url', unique=True,)
...
def __str__(self):
return self.short_name
def get_absolute_url(self):
return reverse('debtor-detail', kwargs={"slug": self.slug})
def save(self, *args, **kwargs):
self.slug = slugify(self.short_name)
super(Debtor, self).save(*args, **kwargs)
class Debtor_List(LoginRequiredMixin, ListView):
login_url = 'signin'
template_name = 'main/debtor_list.html'
context_object_name = 'debtor_list'
paginate_by = 1
allow_empty = True
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Должники'
return context
def get_queryset(self):
return Debtor.objects.filter(user__id=self.request.user.id)
class Debtor_Create(LoginRequiredMixin, CreateView):
login_url = 'signin'
form_class = DebtorCreateForm
template_name = 'main/debtor_create.html'
success_url = reverse_lazy ('debtor-list')
def form_valid(self, form):
form.instance.user = self.request.user
return super(Debtor_Create, self).form_valid(form)
class Debtor_Detail(LoginRequiredMixin, DetailView):
login_url = 'signin'
model = Debtor
template_name = 'main/debtor_detail.html'
context_object_name = 'debtor_detail'
class Debtor_Update(LoginRequiredMixin, UpdateView):
login_url = 'signin'
model = Debtor
form_class = DebtorUpdateForm
template_name = 'main/debtor_update.html'
success_url = reverse_lazy ('debtor-detail')
urlpatterns = [
path('', index, name='home'),
path('debtor/', Debtor_List.as_view(), name='debtor-list'),
path('debtor/create/', Debtor_Create.as_view(), name='debtor-create'),
path('debtor/<str:slug>/', Debtor_Detail.as_view(), name='debtor-detail'),
path('debtor/<str:slug>/update/', Debtor_Update.as_view(), name='debtor-update'),
]
debtor/<str:slug>/
. But if I add buttons (links) to the edit form ( ) to the Debtor_Detail view template , then everything eventually breaks ... when clicking on the debtor, it gives the NoReverseMatch at ... error :{% url 'debtor-update' %}
NoReverseMatch at /debtor/dp-ddsmts/
Reverse for '<b>debtor-update</b>' with arguments '('',)' not found. 1 pattern(s) tried: ['debtor/(?P<slug>[^/]+)/update/$']
Request Method: GET
Request URL: http://127.0.0.1:8000/debtor/dp-ddsmts/
Django Version: 3.2.4
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'debtor-update' with arguments '('',)' not found. 1 pattern(s) tried: ['debtor/(?P<slug>[^/]+)/update/$']
Exception Location: D:\Dev\z-Projects\SEDAK\venv\lib\site-packages\django\urls\resolvers.py, line 694, in _reverse_with_prefix
Python Executable: D:\Dev\z-Projects\SEDAK\venv\scripts\python.exe
Python Version: 3.9.5
Python Path:
['D:\\Dev\\z-Projects\\SEDAK\\sedak',
'C:\\Python395\\python39.zip',
'C:\\Python395\\DLLs',
'C:\\Python395\\lib',
'C:\\Python395',
'D:\\Dev\\z-Projects\\SEDAK\\venv',
'D:\\Dev\\z-Projects\\SEDAK\\venv\\lib\\site-packages']
Server time: Wed, 30 Jun 2021 11:11:22 +0000
name='debtor-update'
'debtor/<str:slug>/update/'
path('debtor/<str:slug>/', Debtor_Detail.as_view(), name='debtor-detail'),
path('debtor/<str:slug>/update/', Debtor_Update.as_view(), name='debtor-update'),
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question