Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 861
klmmkv Avatar asked Dec 03 '25 07:12

klmmkv


1 Answers

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.

like image 151
Daniel Roseman Avatar answered Dec 05 '25 20:12

Daniel Roseman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!