2016-07-12 160 views
0

我想從提交HTML數據的數據庫(PostgreSQL的)。我正在使用Django我學習它。我點擊提交按鈕後,出現以下錯誤Postgres的。數據庫提交從HTML表單

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): 
 
\t userName=forms.CharField(max_length=200) 
 
\t password=forms.CharField(widget=forms.PasswordInput) 
 
\t 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'), 
 
]

我提交後,細節不提交到數據庫表也。

請哪裏是錯誤嗎?任何提示?

+0

太棒了!祝你好運,記住你總是可以問一個描述性的問題,包括你已經嘗試和研究過的[如果]你卡住了! – Sayse

+0

我試過了。請檢查上面編輯的代碼 – Colo

+0

請閱讀[問]。另外,您的'Registration'類沒有任何模型字段 - 只有表單字段。 – Sayse

回答

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/