DJango - 我的forms.py元素没有显示在html模板中

时间:2015-09-02 12:01:33

标签: python django django-models django-forms django-templates

我是Django的新手,正在从事一个项目。我很难设置一个forms.py并将其与我的模板集成。在我上网的各种教程的帮助下,我完成了所有必需的操作,但是我无法在HTML页面上看到我在表单中声明的​​字段。下面是我为每个模块使用的代码。如果有人能帮助我解决这个问题会很棒。

models.py

from django.db import models

class EarlyBirds(models.Model):
    name = models.CharField(max_length=200)
    email = models.CharField(max_length=200)
    contact_number = models.IntegerField()

    def __str__(self):
        return '%s - %s' % (self.name, self.email)

views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render_to_response

from .forms import EarlyBirdsForm

from .models import EarlyBirds

def register(request):
    context = RequestContext(request)
    success=''
    if request.method == 'POST':
        form = EarlyBirdsForm(request.POST)
        if form.is_valid():
            name = request.POST.get('name','')
            email = request.POST.get('email','')
            number = request.POST.get('number','')
            if email:
                email_exist = EarlyBirds.objects.filter(email=email)
                if email_exist:
                    success = 'Thankyou for your intrest! This email is already registered with us. We will get back to you soon.'
                else:
                    eb_obj = EarlyBirds(name=name,email=email,contact_number=number)
                    eb_obj.save()
                    success = 'Thankyou for your intrest! We will get back to you soon.'
            else:
                success = 'Please fill out the required fields'
        else:
            success = form.errors
    else:
        form = EarlyBirdsForm()
    return render_to_response('ComingSoon.html', {'success':success}, context)

forms.py

from django import forms
from django.forms import ModelForm
from app_name.models import EarlyBirds

class EarlyBirdsForm(forms.Form):
    name = forms.CharField(required=True,max_length=100)
    email = forms.CharField(required=True,max_length=100)
    number = forms.IntegerField(required=True)
    class Meta:
        model = EarlyBirds
        fields = ("name", "email", "number")

模板

<html xmlns="http://www.w3.org/1999/xhtml">
<body align="center">
<form method="POST" action="{%url 'comingsoon:register'%}">
{% csrf_token %}
    <div class="header-blog-comingSoon" align="center">
    <!--<form method="post">

        <span>{{ form.as_p }}</span>    
        <br/>    

        <span><button class="comingsoon-Reg" type="submit">Register</button></span>
        <br/><br/>

        <br/><label class="successLabel">{{success}}</label>
    </div>
</form>
</body>
</html>

project.urls.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^ComingSoon/', include('app_name.urls', namespace="comingsoon")),
    url(r'^admin/', include(admin.site.urls)),
]

当我尝试执行此代码时,我能够在html页面上的“注册”按钮中看到所有内容。姓名,电子邮件和联系电话号码的三个文本字段都丢失了。请让我知道我在这里失踪了什么。

2 个答案:

答案 0 :(得分:0)

您忘记将表单添加到您的上下文中:

def register(request):
    ...
    return render_to_response('ComingSoon.html', {'success':success, 'form': form}, context)

答案 1 :(得分:0)

也许你应该尝试这样写你的表格:

class EarlyBirdsForm(forms.ModelForm):
    class Meta:
        model = EarlyBirds
        fields = '__all__'

更简单,更简单。由于您正在使用模型中的所有属性,因此可以将表单直接与模型连接。

CésarBustíos说得对。您没有在模板的上下文字典中添加表单。