我想用modelForm来处理foreignKey,结果出现了这个错误。
ValueError at /3/
The view main.views.ProductDetailView didn't return an
HttpResponse object. It returned None instead.
问题出在Wilaya和commune字段上,因为当我删除它们时,所有的字段都能正常工作。
在views.py中
class ProductDetailView(ModelFormMixin, DetailView):
model = Produit
context_object_name = 'produit'
form_class = CheckoutCreateForm
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context["form"] = self.get_form()
context["wilayas"]= Wilaya.objects.all()
context["communes"]= Commune.objects.all()
return context
def post(self, request, *args, **kwargs):
# self.object = self.get_object()
# form = self.get_form()
form = CheckoutCreateForm(request.POST)
if form.is_valid():
checkout = form.save(commit=False)
checkout.produit = self.get_object()
checkout.prix = self.get_object().price
checkout.save()
wilaya = form.cleaned_data['wilaya']
commune = form.cleaned_data['commune']
quantity = form.cleaned_data['quantity']
nom_du_client = form.cleaned_data['nom_du_client']
prenom_du_client = form.cleaned_data['prenom_du_client']
adresse_du_client = form.cleaned_data['adresse_du_client']
print('jusque la cv')
try:
form = CheckoutCreateForm()
return redirect(f'/{self.get_object().pk}')
except:
return redirect('/')
在models.py中
class Wilaya(models.Model):
name = models.CharField(max_length=30)
def __str__(self):
return self.name
class Commune(models.Model):
Wilaya = models.ForeignKey(Wilaya, on_delete=models.CASCADE)
name = models.CharField(max_length=30)
def __str__(self):
return self.name
class Checkout(models.Model):
produit = models.ForeignKey('Produit', on_delete=models.CASCADE)
prix = models.IntegerField(default=0)
nom_du_client = models.CharField(max_length=40)
prenom_du_client = models.CharField(max_length=40)
adresse_du_client = models.CharField(max_length=40)
date_added = models.DateTimeField(auto_now_add=True)
wilaya = models.ForeignKey(Wilaya, on_delete=models.SET_NULL, null=True, blank=True)
commune = models.ForeignKey(Commune, on_delete=models.SET_NULL, null=True, blank=True)
quantity = models.PositiveIntegerField(default=1)
confirmer = models.BooleanField(default=False)
def __str__(self):
return str(self.produit)
在forms.py中
from django import forms
from .models import Checkout, Commune , Wilaya
class CheckoutCreateForm(forms.ModelForm):
class Meta:
model = Checkout
fields = ['nom_du_client', 'prenom_du_client','adresse_du_client', 'quantity', 'wilaya', 'commune']
html文件中。
<form class="card-body" method='POST' id="checkoutForm" >
{% csrf_token %}
<!--Grid row-->
<div class="row">
<!--Grid column-->
<div class="col-md-6 mb-2">
<!--firstName-->
<div class="md-form ">
<input type="text" name="nom_du_client" id="firstName" class="form-control">
<label for="nom_du_client" class="">First name</label>
</div>
.... rest of the form
<button class="btn btn-primary btn-lg btn-block" type="submit">Continue to checkout</button>
</form>
这个错误是不言自明的。
视图
main.views.ProductDetailView
杳无音信HttpResponse
对象。它返回了None
而不是。
如果你看一下你的代码的缩略版。
class ProductDetailView(ModelFormMixin, DetailView):
def post(self, request, *args, **kwargs):
...
if form.is_valid():
...
try:
...
return redirect(f'/{self.get_object().pk}')
except:
return redirect('/')
你可以注意到这个方法 post()
返回 None
如果表格无效。
一个可能的解决方案是在表格中加入一个 return redirect(...)
在最后也。
class ProductDetailView(ModelFormMixin, DetailView):
def post(self, request, *args, **kwargs):
...
if form.is_valid():
...
try:
...
return redirect(f'/{self.get_object().pk}')
except:
pass
return redirect('/')