您觉得这段代码怎么样?在html中,我有带有名称属性的字段,我引用了那些 在这里命名。我只是自己编造的,还没有看到类似的例子。
def addsupplier(request):
a = request.POST['companyname']
b = request.POST['contactname']
c = request.POST['address']
d = request.POST['phone']
e = request.POST['email']
f = request.POST['country']
Supplier(companyname = a, contactname = b, address = c, phone = d, email = e, country = f).save()
return redirect(request.META['HTTP_REFERER'])
答案 0 :(得分:2)
更清洁的方法是使用Django ModelForms
from django.forms import ModelForm
from .models import Supplier
class SupplierForm(ModelForm):
class Meta:
model = Supplier
fields = '__all__' #or put the fields you want in list
def addsupplier(request):
form = SupplierForm(request.POST or None)
if(form.is_valid()):
form.save()
return redirect(request.META['HTTP_REFERER'])
else:
return render(request, 'template', context)