Home > Enterprise >  Redirecting user to another page after submitting the form using get_absolute_url() method
Redirecting user to another page after submitting the form using get_absolute_url() method

Time:11-04

I'm new to django and I'm following a tutorial trying to create a blog. I'm currently working on a page to add posts to the blog and I want the user to be automatically directed to the post page after submitting the form. I tried using get_absolute_url method but got this error:

NoReverseMatch at /my_blog/add_post/ Reverse for 'post-detail' not found. 'post-detail' is not a valid view function or pattern name.

I checked my code to see if I have done anything wrong but I couldn't notice. I appreciate any help in advance.

models.py

from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse


class Post(models.Model):
    STATUS = [
        (0, 'Drafted'),
        (1, 'Published'),
    ]
    title = models.CharField(max_length=200, unique=True)
    slug = models.SlugField(max_length=200, unique=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    created_on = models.DateTimeField(auto_now_add=True)
    published_on = models.DateTimeField(auto_now=True)
    content = models.TextField()
    status = models.IntegerField(choices=STATUS, default=0)

    class Meta:
        ordering = ['-created_on']

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post-detail', args=(str(self.id)))

urls.py


from django.urls import path
from .views import PostListView, PostDetailView, AddPostView, UpdatePostView
app_name = 'my_blog'
urlpatterns = [
    path('', PostListView.as_view(), name='posts'),
    path('post/<int:pk>', PostDetailView.as_view(), name='post-detail'),
    path('add_post/', AddPostView.as_view(), name='add-post'),
    path('post/edit/<int:pk>', UpdatePostView.as_view(), name='update-post'),

]

views.py

from django.shortcuts import render
from django.views.generic import ListView, DetailView, CreateView, UpdateView
from .models import Post


class PostListView(ListView):
    model = Post
    template_name = 'post_list.html'
    context_object_name = 'latest_post_list'


class PostDetailView(DetailView):
    model = Post
    template_name = 'my_blog/post_detail.html'


class AddPostView(CreateView):
    model = Post
    template_name = 'my_blog/add_post.html'
    fields = ('__all__')


class UpdatePostView(UpdateView):
    model = Post
    template_name = 'my_blog/update_post.html'
    fields = ['title', 'content']

This is my add post file in the template directory add_post.html

{% extends 'base.html' %} {% block content %}
<h1>Add post...</h1>
<form method="post">
  {% csrf_token %} {{ form.as_p }}
  <input type="submit" value="Submit" />
</form>
{% endblock %}

CodePudding user response:

You used an app_name, so you should specify this namespace with:

class Post(models.Model):
    # …

    def get_absolute_url(self):
        return reverse('my_blog:post-detail', args=(self.pk,))

The primary key is also passed in a (singleton) tuple, so (self.pk,), not (self.pk).

  • Related