django表单不在模板中呈现。输入字段不会显示

时间:2016-12-26 08:42:57

标签: python django forms django-forms

我无法在模板中看到django表单。它没有正确呈现。我已尝试过这方面的工作,但表格没有显示出来。尝试在一个新项目中测试相同的代码 - 运行正常,但在这里它不起作用。这{{form.as_p}}没有显示任何内容,即没有输入字段供我输入详细信息并检查其他部分。提前谢谢。

# forms.py

class ContactForm(forms.Form):
    contact_name = forms.CharField(required=True)
    contact_email = forms.EmailField(required=True)
    contact_subject = forms.CharField(required=True)
    content = forms.CharField(
        required=True,
        widget=forms.Textarea
    )`

# views.py

def contact(request):
    form_class = ContactForm


    if request.method == 'POST':
        form = form_class(data=request.POST)

        if form.is_valid():
            contact_name = request.POST.get(
                'contact_name'
            , '')
            contact_email = request.POST.get(
                'contact_email'
            , '')
            contact_subject = request.POST.get(
                'contact_subject'
                , '')
            form_content = request.POST.get('content', '')

            # Email the profile with the
            # contact information
            template = get_template('contact_template.txt')
            context = Context({
                'contact_name': contact_name,
                'contact_email': contact_email,
                'contact_subject' : contact_subject,
                'form_content': form_content,
            })
            content = template.render(context)

            email = EmailMessage(
                "New contact form submission",
                content,
                "Your website" +'',
                ['youremail@gmail.com'],
                headers = {'Reply-To': contact_email }
            )
            email.send()
            return redirect('contact')

    return render(request, 'contact.html', {
        'form': form_class,
    })

相同的模板看起来像这样。

模板

 <section id="contact">
        <div class="container text-center">
            <div class="row text-center">
                <div class="bg-image">
                    <div class="col-md-6 col-md-offset-3 text-center share-text wow animated zoomInDown heading-text">
                        <p class="heading">
                            If you got any questions, please do not hesitate to send us a message.
                        </p>
                    </div>
                </div>
            </div>

                {% block content %}
                <h1>Contact</h1>
                <form role="form" action="" method="post">{% csrf_token %}

                    {{ form.as_p }}

                    <button type="submit">Submit</button>
                </form>
                {% endblock %}
        </div>
    </section>

3 个答案:

答案 0 :(得分:1)

在GET上添加一个用于生成表单的else子句:

def contact(request):

    form_class = ContactForm

    if request.method == 'POST':
        form = form_class(data=request.POST)
        # ... more code from above ...
    else:
        form = form_class() # this is important

    return render(request, 'contact.html', {
        'form': form,  # NOTE: instead of form_class!!!!
    })

答案 1 :(得分:0)

request.method不是POST时,您尚未处理此案例。

您的观点应如下所示:

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            # form handling logic here
            # ...
            # ... 
            return redirect('some-link')
        else:
            return render(request, 'template.html', dict(form=form))
    else:
      form = ContactForm()
      return render(request, 'template.html', dict(form=form))

答案 2 :(得分:0)

您正在将表单类传递给模板而不是表单实例。