gpt4 book ai didi

python - Django:表单处理错误。处理与另一个模型关联的表单

转载 作者:太空宇宙 更新时间:2023-11-03 14:45:33 25 4
gpt4 key购买 nike

我希望能够向教师提交评论。我有 2 个型号:

models.py

class Teacher(models.Model):
user = models.OneToOneField(User, on_delete=models.PROTECT, related_name='Teacher')

class Review(models.Model):
teacher = models.ForeignKey(Teacher)
#other fields

def __str__(self):
return self.name

forms.py

class ReviewForm(forms.ModelForm):
class Meta:
model = Review
#Note no 'teacher' in fields below
fields = ('title','star','body')

views.py 我有以下表单处理:

def teacher_view(request,**kwargs):
teacher = Teacher.objects.get(pk=kwargs['pk'])
#if request.method == 'POST':
#handle the form
review_form = ReviewForm(request.POST, instance=teacher)
review_form.teacher=teacher
if review_form.is_valid():
review_form.save()
return redirect('users:index')
else:

出于某种原因,我似乎也得到了 else 循环。例如。该表格无效。

Urls.py

url(r'^(?P<pk>[0-9]+)/$', views.teacher_view, name='detail')
<小时/>

完整 View .py

def teacher_profile(request, **kwargs):
teacher = Teacher.objects.get(pk=kwargs['pk'])
reviews = Review.objects.filter(teacher_id=kwargs['pk'])
star = Review.objects.filter(teacher_id=kwargs['pk']).aggregate(Avg('star'))
no_of_ratings = Review.objects.filter(teacher_id=kwargs['pk']).count()

if request.method == "POST":
if 'booking' in request.POST:
form = BookingForm(request.POST)
if form.is_valid():
#handle the form
return redirect('users:index')
else:
review_form = ReviewForm()
elif 'review' in request.POST:
review_form = ReviewForm(request.POST)
review_form['teacher']=teacher
if review_form.is_valid():
review_form.save()
return redirect('users:index')
else:
form = BookingForm()
else:
form = BookingForm()
review_form = ReviewForm()
else:
form = BookingForm()
review_form = ReviewForm()

return render(request, "users/teacher_detail.html", context={"form": form,
"review_form":review_form,
"teacher":teacher,
"reviews":reviews,
"star":star,
"no_of_ratings":no_of_ratings,
})

我再次添加了代码。我已从 view.py 中复制粘贴它,但删除了表单处理,在其中我只是定义变量并重定向到带有成功消息的另一个页面。

我认为为什么你的答案中的逻辑是错误的,因为它首先检查表格是否有效,而由于老师失踪而无效。然后我们在检查其是否有效后定义老师。我尝试通过在 form.is_valid() 之前添加 review_form['teacher']=teacher 来操作代码,但总是收到错误:

'ReviewForm' object does not support item assignment

最佳答案

如果 ReviewForm 有效,您似乎想要添加教师:

def teacher_profile(request,**kwargs):
teacher = Teacher.objects.get(pk=kwargs['pk'])
reviews = teacher.review_set.all()
star = reviews.aggregate(Avg('star'))
no_of_ratings = reviews.count()

form = BookingForm()
review_form = ReviewForm()

if request.method == "POST":
if 'booking' in request.POST:
form = BookingForm(request.POST)
if form.is_valid():
#Correct handling
return redirect('users:index')
elif 'review' in request.POST:
review_form = ReviewForm(request.POST)
if review_form.is_valid():
review = review_form.save(commit=False)
review.teacher = teacher
review.save()
return redirect('users:index')
context = {
"form":form,
"review_form":review_form,
"teacher":teacher,
"reviews":reviews,
"star":star,
"no_of_ratings":no_of_ratings,})
return render(request, "users/teacher_detail.html", context)

teacher_detail.html中添加,

{{ review_form.errors }}

关于python - Django:表单处理错误。处理与另一个模型关联的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46258473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com