I have class where is use both, FormView and DetailView. when i send post request i see this log in terminal [06/Apr/2022 14:44:16] "POST /profile/question/1/ HTTP/1.1" 200 6327, but not working post, form_valid and form_invalid methods. does not call these 3 functions and therefore the form is not stored
this is my code ->
class QuestionDetail(FormView, DetailView):
model = Question
form_class = CommentForm
template_name = "Profile/question_detail.html"
context_object_name = "detail"
def dispatch(self, request, *args, **kwargs):
try:
self.object = self.get_object()
except:
return redirect('Profile:error')
self.get_object()
return super(QuestionDetail, self).get(request, *args, **kwargs)
def get_success_url(self):
return self.request.path
def get_context_data(self, *args, **kwargs):
like_exist=bool(Like.objects.filter(user=self.request.user, question=self.get_object()))
dislike_exist=bool(DisLike.objects.filter(user=self.request.user, question=self.get_object()))
self.object=self.get_object()
context = super(QuestionDetail, self).get_context_data(**kwargs)
try:
question = Question.objects.get(id=self.kwargs["pk"])
context['detail'] = question
context['like_ex'] = like_exist
context['dislike_ex'] = dislike_exist
except Http404:
return reverse("Profile:error")
return context
def post(self, request, *args, **kwargs):
print("Not working post method")
def form_invalid(self, form):
print("Error")
return super(QuestionDetail, self).form_invalid(form)
def form_valid(self, form):
print("It's here")
form.instance.user = self.request.user
form.instance.question = self.get_object()
form.save()
return super(QuestionDetail, self).form_valid(form)
i don't understand what happened.
CodePudding user response:
You need to call the post
method in your dispatch
method:
class QuestionDetail(FormView, DetailView):
model = Question
form_class = CommentForm
template_name = "Profile/question_detail.html"
context_object_name = "detail"
def dispatch(self, request, *args, **kwargs):
try:
self.object = self.get_object()
except:
return redirect('Profile:error')
self.get_object()
return self.post(request, *args, **kwargs)
def get_success_url(self):
return self.request.path
def get_context_data(self, *args, **kwargs):
like_exist=bool(Like.objects.filter(user=self.request.user, question=self.get_object()))
dislike_exist=bool(DisLike.objects.filter(user=self.request.user, question=self.get_object()))
self.object=self.get_object()
context = super(QuestionDetail, self).get_context_data(**kwargs)
try:
question = Question.objects.get(id=self.kwargs["pk"])
context['detail'] = question
context['like_ex'] = like_exist
context['dislike_ex'] = dislike_exist
except Http404:
return reverse("Profile:error")
return context
def post(self, request, *args, **kwargs):
print("Not working post method")
def form_invalid(self, form):
print("Error")
return super(QuestionDetail, self).form_invalid(form)
def form_valid(self, form):
print("It's here")
form.instance.user = self.request.user
form.instance.question = self.get_object()
form.save()
return super(QuestionDetail, self).form_valid(form)
Now call form_invalid
and form_valid
in your implementation of post
.
CodePudding user response:
More of a long comment than an explicit answer
It might be better to use SingleObjectMixin
in conjunction with FormView
. Inheriting from two complete CBV classes is probably not supported and might cause headaches after, for example, a Django verion upgrade. Whereas the *Mixin
classes are intended to compound well.
The main thing you have to know about this is that self.object
is not set by including the mixin. You have to explicitly set it, for example by subclassing the setup
method:
def setup( self, request, *args, **kwargs):
super().setup( request, *args, **kwargs)
self.object = self.get_object() # link SingleObjectMixin
Classy CBVs in an invaluable resource for navigating Class-Based Views