0% found this document useful (0 votes)
12 views3 pages

Django Form Widgets Guide

This document provides a step-by-step guide to creating a Django project with a contact form. It includes instructions for setting up the project, creating an app, defining a form, creating a view, setting up a template, configuring URLs, and running the server. The guide is designed to help users implement a basic contact form using Django's form widgets.

Uploaded by

ekta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Django Form Widgets Guide

This document provides a step-by-step guide to creating a Django project with a contact form. It includes instructions for setting up the project, creating an app, defining a form, creating a view, setting up a template, configuring URLs, and running the server. The guide is designed to help users implement a basic contact form using Django's form widgets.

Uploaded by

ekta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Step-by-Step Guide: Django Form Widgets Project

Step 1: Create a Django Project


Open your terminal and run:

django-admin startproject myformproject

cd myformproject

Step 2: Create a Django App


python manage.py startapp contactform

Then, add 'contactform' to INSTALLED_APPS in settings.py.

Step 3: Create the Form


In contactform/forms.py:

from django import forms

GENDER_CHOICES = [

('M', 'Male'),

('F', 'Female'),

('O', 'Other'),

class ContactForm(forms.Form):

name = forms.CharField(...)

email = forms.EmailField(...)

birth_date = forms.DateField(...)

gender = forms.ChoiceField(...)
message = forms.CharField(...)

Step 4: Create the View


In contactform/views.py:

from django.shortcuts import render

from .forms import ContactForm

def contact_view(request):

if request.method == 'POST':

form = ContactForm(request.POST)

if form.is_valid():

print(form.cleaned_data)

else:

form = ContactForm()

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

Step 5: Create the Template


Create a folder: contactform/templates/contactform/

Inside contact.html:

<form method="post">

{% csrf_token %}

{{ form.as_p }}

<button type="submit">Send</button>

</form>

Step 6: Configure URLs


In contactform/urls.py:
from django.urls import path

from .views import contact_view

urlpatterns = [

path('', contact_view, name='contact'),

In myformproject/urls.py:

from django.urls import path, include

urlpatterns = [

path('', include('contactform.urls')),

Step 7: Run the Server


python manage.py runserver

Then open http://127.0.0.1:8000/ in your browser.

You might also like