从detailview提交formview时报错405
Getting an error 405 when submitting formview from detailview
我可以使用详细信息视图、表单视图和邮件。但它给了我一个 405 错误,而不是重定向回页面。请给我一些眼睛。我不明白为什么它不起作用。我最近 3 将 # 从 .net mvc 切换到 python/django。感谢您的帮助。
FORMS.PY
class MessageSellerForm(forms.Form):
contact_name = forms.CharField(max_length=100, required=True)
contact_email = forms.EmailField(required=True)
content = forms.CharField(required=True, widget=forms.Textarea)
VIEWS.PY
class PostDetailView(细节视图):
型号 = 广告
template_name = 'x_ads/ad_detail.html'
def get_context_data(self, **kwargs):
context = super(PostDetailView, self).get_context_data(**kwargs)
context['form'] = MessageSellerForm
return context
class PostDetail(View):
def get(self, request, *args, **kwargs):
view = PostDetailView.as_view()
return view(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
view = PostDetailView.as_view()
return view(request, *args, **kwargs)
class PostMessageSeller(SingleObjectMixin, FormView):
template_name = 'x_ads/ad_detail.html'
form_class = MessageSellerForm
success_url = '/'
subject = 'User message about your ad'
from_email = '********'
to_email = '*********'
message = '**********'
send_mail(subject=subject, message=message, from_email=from_email,
recipient_list=[to_email], fail_silently=False)
model = Ad
def post(self, request, *args, **kwargs):
if not request.user.is_authenticated:
return HttpResponseForbidden()
self.object = self.get_object()
return super().post(request, *args, **kwargs)
ad_detail.html
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-1 btn-sm" type="submit">Message Seller</button>
</div>
</form>
它显示详细模型,因此我使用 ModelFormMixin 在 DetailView 页面中添加了一个 Comment 部分。
这是我的 .py 代码
def get_context_data(self, *args, **kwargs):
context = super(PostDetailView, self).get_context_data(*args, **kwargs)
context['form'] = self.get_form()
return context
def get_absolute_url(self):
return reverse(request, 'basic_app:post_detail', kwargs={'pk':self.pk})
我可以使用详细信息视图、表单视图和邮件。但它给了我一个 405 错误,而不是重定向回页面。请给我一些眼睛。我不明白为什么它不起作用。我最近 3 将 # 从 .net mvc 切换到 python/django。感谢您的帮助。
FORMS.PY
class MessageSellerForm(forms.Form):
contact_name = forms.CharField(max_length=100, required=True)
contact_email = forms.EmailField(required=True)
content = forms.CharField(required=True, widget=forms.Textarea)
VIEWS.PY class PostDetailView(细节视图): 型号 = 广告 template_name = 'x_ads/ad_detail.html'
def get_context_data(self, **kwargs):
context = super(PostDetailView, self).get_context_data(**kwargs)
context['form'] = MessageSellerForm
return context
class PostDetail(View):
def get(self, request, *args, **kwargs):
view = PostDetailView.as_view()
return view(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
view = PostDetailView.as_view()
return view(request, *args, **kwargs)
class PostMessageSeller(SingleObjectMixin, FormView):
template_name = 'x_ads/ad_detail.html'
form_class = MessageSellerForm
success_url = '/'
subject = 'User message about your ad'
from_email = '********'
to_email = '*********'
message = '**********'
send_mail(subject=subject, message=message, from_email=from_email,
recipient_list=[to_email], fail_silently=False)
model = Ad
def post(self, request, *args, **kwargs):
if not request.user.is_authenticated:
return HttpResponseForbidden()
self.object = self.get_object()
return super().post(request, *args, **kwargs)
ad_detail.html
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-1 btn-sm" type="submit">Message Seller</button>
</div>
</form>
它显示详细模型,因此我使用 ModelFormMixin 在 DetailView 页面中添加了一个 Comment 部分。
这是我的 .py 代码
def get_context_data(self, *args, **kwargs):
context = super(PostDetailView, self).get_context_data(*args, **kwargs)
context['form'] = self.get_form()
return context
def get_absolute_url(self):
return reverse(request, 'basic_app:post_detail', kwargs={'pk':self.pk})