S
S
Shua_inc2019-02-12 18:19:28
Django
Shua_inc, 2019-02-12 18:19:28

I am writing a bike in Django (blogs). How to make a redirect to the created post?

I rewarded this:

class PostCreateView(LoginRequiredMixin, generic.CreateView):
    model = Post
    template_name = 'blog/post_form.html'
    form_class = PostCreationForm

    def form_valid(self, form):
        post = form.save(commit=False)
        post.author = self.request.user
        post.save()
        return HttpResponseRedirect(self.get_success_url(post.id))

    def get_success_url(self, id):
        return reverse('post-detail',args=(id,))

But I don't like it, I feel that it can be prettier :) Can I somehow do without passing id to get_success_url() ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-02-12
@Shua_inc

from django.shortcuts import redirect

...
return redirect(post)

And in the model, define the methodget_absolute_url

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question