django form not rendering in template. Input field

2019-06-08 06:17发布

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.

template

 <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条回答
家丑人穷心不美
2楼-- · 2019-06-08 07:07

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!!!!
    })
查看更多
爷、活的狠高调
3楼-- · 2019-06-08 07:08

You haven't handled the case when request.method is not POST.

Your view should look like this:

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))
查看更多
叼着烟拽天下
4楼-- · 2019-06-08 07:15

You are passing the form class to your template instead of a form instance.

查看更多
登录 后发表回答