我在使用 ModelForm 将行插入 sqlite 时遇到问题。我真的不知道我做错了什么。有什么建议么?谢谢!这是我的看法:
def row(request):
if request.method == 'POST':
form = Form(request.POST or None)
if form.is_valid():
new_publisher=form.save
name = form.cleaned_data['name']
address = form.cleaned_data['address']
city = form.cleaned_data['city']
website = form.cleaned_data['website']
p=Publisher(
name=name,
city=city,
address=address,
website =website,
state_province='example_content1',
country='example_content2'
)
p.save
results=Publisher.objects.all()
else:
form.errors
return render_to_response('after_subbmission.html', locals(),context_instance=RequestContext(request))
这是我的模型:
class Publisher(models.Model):
name = models.CharField(max_length=30)
address=models.CharField(max_length=50)
city=models.CharField(max_length=60)
state_province=models.CharField(max_length=30)
country=models.CharField(max_length=30)
website=models.URLField()
def __str__(self):
return self.name
这是我的表格:
class Form(ModelForm):
class Meta:
model=Publisher
这是我的html:
<form action="/row/" enctype="multipart/form-data" method="POST">{% csrf_token %}
<table style="border:solid 1px black; width:300px";>
<tr>
<th style="border:solid 1px black";>name</th><th style="border:solid 1px black";>adress</th>
<th style="border:solid 1px black";>city</th>
<th style="border:solid 1px black";>website</th>
<th style="border:solid 1px black";>id</th>
<th style="border:solid 1px black";>State Province</th>
<th style="border:solid 1px black";>Country</th>
<tr>
<td style="border:solid 1px black";><input type="text" name="name" value= {{choose.name}}></td><td style="border:solid 1px black";><input type="text" name="address" value={{choose.address}} ></td>
<td style="border:solid 1px black";><input type="text" name="city" value={{choose.city}} ></td>
<td style="border:solid 1px black";><input type="text" name="website" value={{choose.website}} ></td>
<td style="border:solid 1px black";><input type="text" name="id" value={{choose.id}} ></td>
<td style="border:solid 1px black";><input type="text" name="state_province" value={{choose.state_province}} ></td>
<td style="border:solid 1px black";><input type="text" name="country" value={{choose.country}} ></td>
</tr>
</table>
<input type="submit" value="save" />
</form>
当然,我已经导入了所有必要的类和表单,提交后没有错误。