Z
Z
zimmion2021-06-30 15:21:16
Django
zimmion, 2021-06-30 15:21:16

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)


Views.py :
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')


URLs.py :
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'),
  ]


The program works out the display of the list of debtors ( Debtor_List ) with links, creates new debtors ( Debtor_Create ), looks through the information on a single debtor ( Debtor_Detail ) with the construction of the correct link by the slug - 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' %}
spoiler

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



As I understand it, the slug parameter is not passed to the named link, and therefore Django cannot build the link correctly . What should be done to make the UpdateView pass the slug argument to build the route??? and for Django to work correctly on named references: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 question

Ask a Question

731 491 924 answers to any question