Answer the question
In order to leave comments, you need to log in
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,))
Answer the question
In order to leave comments, you need to log in
from django.shortcuts import redirect
...
return redirect(post)
get_absolute_url
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question