尝试提交视图并收到 405 错误
trying to submit a view and getting 405 error
我正在尝试在用户个人资料中实施评论部分,但是当我尝试提交评论时,我总是收到此 405 错误,即使当我在管理面板中创建评论时,它也会正常显示在个人资料中。如果你能帮忙。
提前致谢
代码:
型号
class Review(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
expert = models.ForeignKey(Expert, on_delete=models.CASCADE)
timestamp = models.DateTimeField(auto_now_add=True)
content = models.TextField()
表格
class ReviewForm(forms.ModelForm):
content = forms.CharField(widget=forms.Textarea(attrs={
'rows':3,
}))
class Meta:
model = Review
fields = ('content',)
查看
class ExpertDetailView(DetailView):
model = Expert
def expert(self, *args, **kwargs):
form = ReviewForm(self.request.POST)
if form.is_valid():
expert = self.get_object()
review = form.instance
review.user = self.request.user
review.expert = expert
review.save()
print ('worked')
print ('worked')
def get_object(self, **kwargs):
object = super().get_object(**kwargs)
return object
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'form': ReviewForm()
})
return context
最后是模板
<h4>Leave a comment below</h4>
<form method='POST'>
{% csrf_token %}
{{ form|crispy}}
<br>
<button class='btn btn-primary' type='submit'>review</button>
</form>
<hr />
<h5>Comments</h5>
{% for review in object.reviews %}
<div>
<p>{{ review.content }}
<br />
<small>{{ review.timestamp|timesince }} ago</small>
</div>
<hr />
{% endfor %}
A 405 表示方法不被允许。这是有道理的,因为您没有写 def post(request, *args, **kwargs)
,而 DetailView
默认情况下只会写
也就是说,自己实现所有逻辑可能不是一个好主意。您可以让 ModelFormMixin
已经让您完成大部分工作:
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic.edit import ModelFormMixin
class ExpertDetailView(LoginRequiredMixin, ModelFormMixin, DetailView):
model = Expert
form_class = ReviewForm
success_url = <i>'some-url'</i>
def <b>post</b>(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
review = form.instance
review.user = self.request.user
review.expert = <b>self.object</b>
form.save()
return super().form_valid(form)
Note: You can limit views to a class-based view to authenticated users with the
LoginRequiredMixin
mixin [Django-doc].
我正在尝试在用户个人资料中实施评论部分,但是当我尝试提交评论时,我总是收到此 405 错误,即使当我在管理面板中创建评论时,它也会正常显示在个人资料中。如果你能帮忙。
提前致谢
代码:
型号
class Review(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
expert = models.ForeignKey(Expert, on_delete=models.CASCADE)
timestamp = models.DateTimeField(auto_now_add=True)
content = models.TextField()
表格
class ReviewForm(forms.ModelForm):
content = forms.CharField(widget=forms.Textarea(attrs={
'rows':3,
}))
class Meta:
model = Review
fields = ('content',)
查看
class ExpertDetailView(DetailView):
model = Expert
def expert(self, *args, **kwargs):
form = ReviewForm(self.request.POST)
if form.is_valid():
expert = self.get_object()
review = form.instance
review.user = self.request.user
review.expert = expert
review.save()
print ('worked')
print ('worked')
def get_object(self, **kwargs):
object = super().get_object(**kwargs)
return object
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'form': ReviewForm()
})
return context
最后是模板
<h4>Leave a comment below</h4>
<form method='POST'>
{% csrf_token %}
{{ form|crispy}}
<br>
<button class='btn btn-primary' type='submit'>review</button>
</form>
<hr />
<h5>Comments</h5>
{% for review in object.reviews %}
<div>
<p>{{ review.content }}
<br />
<small>{{ review.timestamp|timesince }} ago</small>
</div>
<hr />
{% endfor %}
A 405 表示方法不被允许。这是有道理的,因为您没有写 def post(request, *args, **kwargs)
,而 DetailView
默认情况下只会写
也就是说,自己实现所有逻辑可能不是一个好主意。您可以让 ModelFormMixin
已经让您完成大部分工作:
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic.edit import ModelFormMixin
class ExpertDetailView(LoginRequiredMixin, ModelFormMixin, DetailView):
model = Expert
form_class = ReviewForm
success_url = <i>'some-url'</i>
def <b>post</b>(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
review = form.instance
review.user = self.request.user
review.expert = <b>self.object</b>
form.save()
return super().form_valid(form)
Note: You can limit views to a class-based view to authenticated users with the
LoginRequiredMixin
mixin [Django-doc].