[英]Django - saving form to the built in database SQLite
这是我第一次使用Django,非常简单地尝试将文本保存到数据库中。 我已经在数据库中创建了表输入。
我收到以下错误;
我的代码如下:
型号
from django.db import models
class Input(models.Model):
waist = models.IntegerField(default=0)
height = models.IntegerField(default=0)
def __unicode__(self):
return "{0} {1} {2}".format(
self, self.waist, self.height)
表格
class InputForm(forms.ModelForm):
class Meta:
model = Input
fields ={
'waist',
'height'
}
views.py
def InputView(request):
if request.POST:
form = InputForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('account/input')
else:
form = InputForm()
args = {'form' : form}
return render(request,'accounts/input.html', args)
urls.py
url(r'^input/$',views.InputView, name='view_input')
input.html
{% extends 'base.html' %}
{% block head %}
<title> Edit Profile </title>
{% endblock %}
{% block body %}
<div class="container">
<h1> Enter Body Details </h1>
<br>
<br>
<form action="account/input" method="post">
{% csrf_token %}
<ul>
{{form.as_ul}}
</ul>
<input type="Submit" name="submit" value="submit"/>
</form>
</div>
{% endblock %}
如果有人可以帮助,将不胜感激。
HttpResponseRedirect('account/input')
您需要在开头再添加一个“ /”,例如
HttpResponseRedirect('/account/input')
另一种方法是使用reverse(),因此,如果您更改URL,则不必更改代码,并且可以避免输入URL时出错。
代替
HttpResponseRedirect('/account/input')
采用
HttpResponseRedirect(reverse('view_input'))
记得添加导入
from django.urls import reverse
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.