POST 方法在我使用 Formview 的 Django 中不起作用
POST method not working in django, where i use Formview
我有 class 同时使用 FormView 和 DetailView。当我发送 post 请求时,我在终端 [06/Apr/2022 14:44:16] 中看到此登录“POST /profile/question/1/ HTTP/1.1" 200 6327,但无法使用 post、form_valid 和 form_invalid 方法。
不调用这 3 个函数,因此不存储表单
这是我的代码 ->
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)
我不明白发生了什么。
您需要在您的dispatch
方法中调用post
方法:
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)
现在在 post
的实现中调用 form_invalid
和 form_valid
。
长评论多于明确回答
最好结合使用 SingleObjectMixin
和 FormView
。从两个完整的 CBV 类 继承可能不受支持,并且可能会在例如 Django verion 升级后引起头痛。而 *Mixin
类 旨在很好地复合。
你必须知道的主要事情是 self.object
不是通过包含 mixin 设置的。您必须明确设置它,例如通过子类化 setup
方法:
def setup( self, request, *args, **kwargs):
super().setup( request, *args, **kwargs)
self.object = self.get_object() # link SingleObjectMixin
Classy CBVs 在用于导航 Class-Based 视图
的宝贵资源中
我有 class 同时使用 FormView 和 DetailView。当我发送 post 请求时,我在终端 [06/Apr/2022 14:44:16] 中看到此登录“POST /profile/question/1/ HTTP/1.1" 200 6327,但无法使用 post、form_valid 和 form_invalid 方法。 不调用这 3 个函数,因此不存储表单
这是我的代码 ->
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)
我不明白发生了什么。
您需要在您的dispatch
方法中调用post
方法:
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)
现在在 post
的实现中调用 form_invalid
和 form_valid
。
长评论多于明确回答
最好结合使用 SingleObjectMixin
和 FormView
。从两个完整的 CBV 类 继承可能不受支持,并且可能会在例如 Django verion 升级后引起头痛。而 *Mixin
类 旨在很好地复合。
你必须知道的主要事情是 self.object
不是通过包含 mixin 设置的。您必须明确设置它,例如通过子类化 setup
方法:
def setup( self, request, *args, **kwargs):
super().setup( request, *args, **kwargs)
self.object = self.get_object() # link SingleObjectMixin
Classy CBVs 在用于导航 Class-Based 视图
的宝贵资源中