1👍
✅
You can override .form_valid(…)
[Django-doc] to run when the form is valid, and before the form is saved and a redirect is made:
class CreatePostView(CreateView):
model = Post
form_class = PostForm
template_name = 'create_post.html'
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
Note: You can limit views to a class-based view to authenticated users with the
LoginRequiredMixin
mixin [Django-doc].
Source:stackexchange.com