[PYTHON] Django contact form

Premise

Gmail only

Django installed

Project name: sample App name: contact

Main subject

I will implement it immediately.

After installing Django, create a project.

$ django-admin startproject project

After moving to the project </ code> directory with the cd command, create the app directory.

$ python3 manage.py startapp contact

We will modify settings.py </ code>.

sample/project/settings.py


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'contact', #Postscript
]

LANGUAGE_CODE = 'ja' #Change
TIME_ZONE = 'Asia/Tokyo' #Change

Edit so that the app is linked to urls.py </ code> in the same directory as settings.py </ code>.

sample/project/urls.py


from django.contrib import admin
from django.urls import path, include #Postscript

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('contact.urls')), #Postscript
]

Next, edit the contact </ code> directory. Create a new urls.py </ code> and write the URL pattern.

sample/contact/urls.py


from django.urls import path
from . import views

app_name = 'contact'
urlpatterns = [
        path('', views.index, name='index'),
        path('contact_form/', views.contact_form, name='contact_form'),
        path('contact_form/contact/complete/', views.complete, name='complete'),
]

Edit views.py </ code> in the contac directory to associate with the template.

sample/contact/views.py


from django.shortcuts import render


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

#Inquiry form screen
def contact_form(request):
    return render(request, 'contact/contact_form.html')

#Transmission completion screen
def complete(request):
    return render(request, 'contact/complete.html')

Next is the creation of the template. Create a new directory (folder) called templates </ code> in the contact directory, Create a ** contact directory ** in the templates directory Create index.html </ code> in the contact directory.

sample/contact/templates/contact/index.html


<!doctype html>
<html>
  <head>
    <title>Website</title>
  </head>
  <body>

<center>
<a href="{% url 'contact:contact_form' %}">
<h2>Inquiry form</h2>
</a>
</center>
</body>
</html>

Next is the inquiry form.

sample/contact/templates/contact/contact_form.html


<center>
<h1>input form</h1><hr><br>
</center>

This is the transmission completion screen.

sample/contact/templates/contact/complete.html


<center>
<h1>send completely</h1>
<a href="{% url 'contact:index' %}">
<p>To the top</p>
</a>
</center>

Form creation

Display the template using Django's Form class. Create a new forms.py </ code> in the contact directory.

sample/contact/forms.py


from django import forms

class ContactForm(forms.Form):
    subject = forms.CharField(label='subject', max_length=100)
    sender = forms.EmailField(label='Email', help_text='* Please check and enter correctly.')
    message = forms.CharField(label='message', widget=forms.Textarea)
    myself = forms.BooleanField(label='Receive the same content', required=False)

■ subject: Subject ■ sender: Email input ■ help_text: A note that will be displayed later in the template. ■ message: By setting widget = forms.Textarea </ code> to the content to be written You will be passed a text box that allows you to write multiple lines. ■ myself: Check box.

Process the created ContactForm with contact_form </ code> in views.py </ code> and Make it visible in the template.

sample/contact/views.py


from django.shortcuts import render, redirect #Postscript
from .forms import ContactForm #Postscript

........
........
def contact_form(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            return redirect('contact:complete')
    else:
        form = ContactForm()
    return render(request, 'contact/contact_form.html', {'form': form})
.........
.........

If the contents of the POST </ code> form are verified with form.is_valid () </ code> and correct It is a description that it is passed to complete.html </ code>.

Next, I will describe it for display. Edit contact_form.html </ code>.

sample/templates/contact/contact_form.html


<center>
<h1>input form</h1><hr><br>

<!--Postscript-->
<form action="{% url 'contact:contact_form' %}" method="post">
    {% csrf_token %}
<table>
    {{ form.as_table }}
</table><br>
    <input type="submit" value="Send">
</form>
</center>

{% csrf_token%} </ code> is a cross-site request forgery (CSRF) countermeasure. Without this, an error will occur.

I will write about Gmail at a later date.