klmmkv
klmmkv

Reputation: 69

Django Form not showing, only the button (NEW)

only my Submit button is popping up when i am trying to pass a form. I even tried just copying the code from the Django website... Still does not work for me.

This is my forms.py

from django import forms

class contactForm(forms.Form):
    name = forms.CharField(required=False, max_length=100, help_text='100 max.')
    email = forms.EmailField(required=True)
    comment = forms.CharField(required=True, widget=forms.Textarea)

This is my views.py

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from .forms import contactForm

def contact(request):
    form = contactForm(request.POST or None)

    if form.is_valid():
       print(request.POST)
    context = locals()

    return render(request, 'contact/contact.html', context)

def index(request):
    return render(request, 'contact/contact.html')

This is my contact.html

{% extends "contact/base.html" %}
{% block content %}

<h1>Contact</h1>

<form method='POST' acion=''> {% csrf_token %}
{{ form.as_p }}
<input type='submit' value='submit form'/>
</form>

{% endblock %}

I really do not know what is wrong with this code...please help! Thanks ps: I am new to python, maybe i need another version?

Upvotes: 3

Views: 782

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599450

You have two views rendering the same template. One of them, contact, passes the form to the template. The other, index, does not, so there is nothing called form in the context and the result will be blank.

Upvotes: 5

Related Questions