无法让'Form'在Django中工作

时间:2015-09-27 17:19:50

标签: python django sqlite mobile-website

我正在使用Django / Python创建一个站点。我刚刚开始注册页面,用户将在其中输入信息,数据应该流入Django附带的sqlite3数据库。我从昨天晚上(现在已经不到24小时)试图找出为什么我的表格没有出现。我尝试将我得到的东西与我的目标相提并论,但是没有足够的声望点 请访问:https://www.youtube.com/watch?v=yfjhLKL-_5Q 9.09分钟,我的目标是什么。请注意:youtube vid上的“welcome”一词已被“谢谢”替换为我。这段代码在我的views.py中。

问题在于,用户在视频中输入其姓名和电子邮件的小方框(我已将其替换为Profession,first_name,last_name等)根本没有出现......相反,我只是得到了'谢谢你'出现'我的'用户名'(相当于jmitchel3)和注册按钮,但没有请求文本框。我的代码如下。

我最初认为我的表单设置不正确所以我将我的代码与成功的页面进行了比较,但没有运气。我为模型做了同样的事,但在那里看不到任何东西......

models.py:

from django.db import models

# Create your models here.
class Register(models.Model):

    Profession = models.CharField(max_length=120)
    first_name = models.CharField(max_length=120)
    last_name = models.CharField(max_length=120)
    email = models.EmailField(max_length = 120, unique = True)
    phone_number = models.CharField(max_length=120)
    instgram_ID = models.CharField(max_length=120, blank=True, null=True)
    facebook_ID = models.CharField(max_length=120, blank=True, null=True)
    twitter_ID = models.CharField(max_length=120, blank=True, null=True)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __unicode__(self):
        return self.email

    forms.py

    from django import forms

from .models import Register

class RegisterForm(forms.ModelForm):
    class Meta: 
        model = Register
        fields = ['first_name', 'last_name', 'email', 'phone_number', 'instgram_ID', 'facebook_ID', 'twitter_ID']

    def clean_email(self):
        email = self.cleaned_data.get('email')
        email_base, provider = email.split("@")
        domain, extension = provider.split('.')
        #if not domain = 'USC':
        #   raise forms.ValidationError("Please make sure you use your USC email.")
        #if not extension == "edu":
        #   raise forms.ValidationError("Please use a valid .EDU email address")

            #return email

        def clean_full_name(self):
            full_name = self.cleaned_data.get('full_name')
            #write validation code.
            return full_name

        admin.py

        from django.contrib import admin

    # Register your models here.

    from .forms import RegisterForm
    from .models import Register

    class RegisterAdmin(admin.ModelAdmin):
        list_display = ["__unicode__", "timestamp", "updated"]
        form = RegisterForm
        #class Meta:
        #   model = Register
    )

    admin.site.register(Register, RegisterAdmin

views.py

from django.shortcuts import render

from .forms import RegisterForm
# Create your views here.
def home(request):
    title = 'Welcome'
    form = RegisterForm(request.POST or None)
    context = {
    "title": title, 
    "form": form 
    }

    if form.is_valid():
        instance = form.save(commit=False)
        if not instance.full_name:
            instance.full_name = "Justin"
        instance.save

    context = {
        "title": "Thank you"
        }

    return render(request, "home.html", context)

home.html:

<h1>{{title}}</h1>

{{ user }}
{{ request.user }}

<form method ='POST' action=''>{% csrf_token %}

{{ form }}

<input type='submit' value = 'Register'>

<!-- <input> type='text'> -->
<!-- <input> -->

    </form>

我已经有一段时间了,所以如果我需要添加其他内容,请告诉我..

1 个答案:

答案 0 :(得分:0)

您有缩进错误。仅context替换{"title": "Thank you"}的部分应该在is_valid()块中缩进。现在它正在取代早期的上下文定义,结果是你不会将表单发送到模板。