1👍
You need to set the .publisher
of the post, so:
class PostCreateView(LoginRequiredMixin,CreateView):
model=Post
fields = ['title','description']
redirect_field_name = 'login'
def form_valid(self, form):
form.instance.publisher = self.request.user
return super().form_valid(form)
You probably should also specify the success_url
[Django-doc] to specify where to redirect to if the POST request was successful.
Note: It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly. For more information you can see the referencing theUser
model section of the documentation.
Source:stackexchange.com