简体   繁体   中英

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. I have created the table inputs in the database.

I am getting the following error;

Error - Page not found (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

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 %}

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.

Instead of

HttpResponseRedirect('/account/input')

use

HttpResponseRedirect(reverse('view_input'))

remember to add the import

from django.urls import reverse

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM