django表单不在模板中呈现。输入字段不显示 [英] django form not rendering in template. Input fields doesn't shows up

查看:174
本文介绍了django表单不在模板中呈现。输入字段不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm not able to see the django form in my template. it is not being rendered properly. I've tried working on this, but the form not shows up. Tried the same code in a new project to test-that worked fine but here it doesn't work. This {{ form.as_p }} not shows up anything i.e. no input fields for me to enter the details and check the other part. Thanks in advance.

# 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
    )`



<

and:

# 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,
    })

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

The template for the same looks like this.

 <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>


推荐答案

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

Add an else clause for generating a form on GET:

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!!!!
    })

这篇关于django表单不在模板中呈现。输入字段不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆