[英]Django - saving form to the built in database SQLite
This is my first time using Django and I am very simply trying to save text to the database. 这是我第一次使用Django,非常简单地尝试将文本保存到数据库中。 I have created the table inputs in the database. 我已经在数据库中创建了表输入。
I am getting the following error; 我收到以下错误;
Error - Page not found (404) 错误-找不到页面(404)
My code is as follows; 我的代码如下:
Models.py 型号
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)
forms.py 表格
class InputForm(forms.ModelForm):
class Meta:
model = Input
fields ={
'waist',
'height'
}
views.py 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 urls.py
url(r'^input/$',views.InputView, name='view_input')
input.html 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 %}
If any one can help it would be greatly appreciated. 如果有人可以帮助,将不胜感激。
HttpResponseRedirect('account/input')
您需要在开头再添加一个“ /”,例如
HttpResponseRedirect('/account/input')
Another way to do it is to use reverse() so if you change the URL you don't have to change your code and you avoid mistakes entering the URL. 另一种方法是使用reverse(),因此,如果您更改URL,则不必更改代码,并且可以避免输入URL时出错。
Instead of 代替
HttpResponseRedirect('/account/input')
use 采用
HttpResponseRedirect(reverse('view_input'))
remember to add the import 记得添加导入
from django.urls import reverse
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.