Home > Mobile >  Page not found - The current path, matched the last one
Page not found - The current path, matched the last one

Time:11-02

I have created an update view. I want to add a button to the post that directs to the update view. However when you click the button you get this error.

404 Page not found
post/<int:pks>/build-log/<int:pk>/update/ [name='build-log-update']
post/75/build-log/127/update/, matched the last one.`

The reason for this error occurring is because it is flipping the PK when you click the button. Example. /post/127/build-log/75/ after button clicked /post/75/build-log/127/update/ you can see it is flipping the PK.

If just just append update to the good url it works fine I cannot figure out why it is flipping the pks

html:

<a class="delete-btn" href='{% url "build-log-delete" pk=log.post_id pkz=log.pk %}'>Delete</a>
<a class="update-btn" href='{% url "build-log-update" pk=log.post_id pkz=log.pk %}'>Update</a>

urls:

path('post/<int:pk>/build-log/<int:pkz>/', views.BuildLogDisplay, name='build-log-view'),
path('post/<int:pk>/build-log/<int:pkz>/delete/', views.BuildLogDelete, name='build-log-delete'), 
path('post/<int:pk>/build-log/<int:pkz>/update/', UpdateBuildLog.as_view(), name='build-log-update')

model:

class BuildLog(models.Model):
    title = models.CharField(max_length=100)
    content = RichTextUploadingField(blank=True, null=True)
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    post = models.ForeignKey('Post', on_delete=models.CASCADE)

    def get_absolute_url(self):
        return reverse('build-log-view', kwargs={'pkz': self.pk})

view:

class UpdateBuildLog(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = BuildLog
    form_class = BuildLogupdateForm
    template = 'blog/buildlog_update.html'
    

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)
            


    def test_func(self):
        post = self.get_object()
        if self.request.user.id == post.author_id:
            return True
        return False    

CodePudding user response:

The base post URL and the delete URL have pk as the first captured value, while the update URL has pk as the second captured value.

'post/<int:pk>/build-log/<int:pkz>/'
'post/<int:pk>/build-log/<int:pkz>/delete/'
'post/<int:pks>/build-log/<int:pk>/update/'

CodePudding user response:

I had to pass this on the view.

def get_object(self):
return BuildLog.objects.get(pk=self.kwargs["pkz"])
  • Related