数据库从HTML表单提交到postgres

时间:2016-07-12 14:11:43

标签: django

我想将数据从html提交到数据库(postgresql)。我正在使用django,因为我学习它。点击提交按钮后出现以下错误。

error

表格是:

<form action="\polls\Registration" method="POST">
              <div class="form-group mb15">
                <input type="text" class="form-control" name="userName" placeholder="Enter Your Username" required>
              </div>
              <div class="form-group mb15">
                <input type="text" class="form-control" name="password" placeholder="Enter Your Password">
              </div>
              <div class="form-group mb15">
                <input type="text" class="form-control" name="fullName" placeholder="Enter Your Full Name">
              </div>
              <div class="form-group mb20">
                <label class="ckbox">
                  <input type="checkbox" name="checkbox">
                  <span>Accept terms and conditions</span>
                </label>
              </div>
              <div class="form-group">
                <button class="btn btn-success btn-quirk btn-block">Create Account</button>
                <br>
                <a href="/polls/signin" class="btn btn-default btn-quirk btn-stroke btn-stroke-thin btn-block btn-sign">Already a member? Sign In Now!</a>
              </div>
            </form>

模型是:

from __future__ import unicode_literals

from django.db import models

# Create your models here.
from django.db import models
from django import forms

class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

    def __str__(self):              # __unicode__ on Python 2
        return self.name

class Author(models.Model):
    name = models.CharField(max_length=50)
    email = models.EmailField()

    def __str__(self):              # __unicode__ on Python 2
        return self.name

class Entry(models.Model):
    blog = models.ForeignKey(Blog)
    headline = models.CharField(max_length=255)
    body_text = models.TextField()
    pub_date = models.DateField()
    mod_date = models.DateField()
    authors = models.ManyToManyField(Author)
    n_comments = models.IntegerField()
    n_pingbacks = models.IntegerField()
    rating = models.IntegerField()

    def __str__(self):              # __unicode__ on Python 2
        return self.headline

class Registration(models.Model):
	userName=forms.CharField(max_length=200)
	password=forms.CharField(widget=forms.PasswordInput)
	fullName=forms.CharField(max_length=250)

urls.py

from django.conf.urls import url
from django.contrib import admin
from . import myview 
from . import view
from . import models
from django.conf.urls import include,url
urlpatterns = [
    url(r'^$', myview.index,name='index'),
    url(r'^admin/', admin.site.urls),
    url(r'^accounts/', include('registration.backends.simple.urls')),
    url(r'^accounts/', include('registration.backends.model_activation.urls')),
    #url(r'^$',myview.index1, name='index1'),
    #url(r'^$',view.index2, name='index2'),
    url(r'^index', myview.index,name='index'),
    url(r'^index2', myview.index2,name='index2'),
    url(r'^signup', myview.signup,name='signup'),
    url(r'^signin', myview.signin,name='signin'),
    url(r'^logiin', myview.login,name='login'),
    url(r'^auth', myview.auth_view,name='auth_view'),
    url(r'^signout', myview.signout,name='signout'),
    url(r'^Registration', models.Registration,name='Registration'),
]

提交后,详细信息也不会提交到数据库表。

请问错误在哪里?有任何提示吗?

1 个答案:

答案 0 :(得分:1)

您需要创建注册视图。

url()函数需要一个视图,并将在该视图上调用get函数。 目前,您正在为注册网址提供运行模型。它试图运行get方法,但你的模型没有,因此错误。

在您的视图文件中尝试以下内容:

class RegistrationView(FormView):
    model = Registration
    template_name = 'path to your tamplate here'
    def get(self, *args, **kwrags):
         # code here to display the empty form
    def post(self, *args, **kwrags):
         # code here to handle the request with a populated form. 

该示例适用于基于类的FormView。 https://docs.djangoproject.com/en/1.9/ref/class-based-views/generic-editing/#formview

使用基于函数的视图开始可能更容易(更多打字,更少'魔术')。 阅读相关文档可能是一个好主意: https://docs.djangoproject.com/en/1.9/topics/forms/