[PYTHON] What is a dog? Django--Getting Started with Form for the First Time POST Transmission Volume

Try sending with Form

Hello! This is Ponta, a Shiba Inu. I had my body washed at will in the bathroom today. After that, I felt strange that I wasn't myself, so I just licked my whole body.

Well, today I'm going to challenge the form.

Write form in template

wan/templates/wan/index.html


<!DOCTYPE html>
<html lang="ja">
<head>
	<meta charset="utf-8">
	<title>{{ title }}</title>
</head>
<body>
	<h1>{{ title }}</h1>
	<p>{{ message }}</p>
	<form action="{% url 'wan:form' %}" method="POST">
		{% csrf_token %}
		<label for="message">message: </label>
		<input id="message" type="text" name="message">
		<input type="reset" value="cancel">
		<input type="submit" value="click">
	</form>
</body>
</html>

The form is described. There is {% url'wan: form'%}, but wan: form in this represents the destination of

, and represents the address registered with name in the url patterns of wan / urls.py. I will. Of wan: form, wan is the application name registered in wan / urls.py.

Registration of app name and setting of url patterns

wan/urls.py


from django.urls import path
from . import views

app_name = 'wan'

urlpatterns = [
    path('', views.index, name='index'),
    path('form', views.form, name='form'),
]

That's why. The app name wan is registered with app_name ='wan'.

views.py Define the form here.

views.py


from django.shortcuts import render

def index(request):
    params = {
        'title': 'Wan/Index',
        'message': "What's your message?",
    }
    return render(request, 'wan/index.html', params)

def form(request):
    message = request.POST['message']
    params = {
        'title': 'Wan/Index',
        'message': message,
    }
    return render(request, 'wan/index.html', params)

Display test

スクリーンショット 2020-08-28 22.32.52.png

Enter the message as follows:

スクリーンショット 2020-08-28 22.33.41.png

Click click ...

スクリーンショット 2020-08-28 22.34.49.png

I was able to enter a message, deliver it in a form, receive it and display it, and then flow.

See you dear! Bye bye!

Recommended Posts

What is a dog? Django--Getting Started with Form for the First Time POST Transmission Volume
What is a dog? POST Sending Volume Using Django--forms.py
What is a dog? Django installation volume
What is a dog? Python installation volume
Let's display a simple template that is ideal for Django for the first time
What I got into Python for the first time
Register a task in cron for the first time
Import audit.log into Splunk and check the behavior when Splunk is started for the first time
Kaggle for the first time (kaggle ①)
Introducing yourself at Qiita for the first time (test post)
Kaguru for the first time
What is the interface for ...
What I learned by writing a Python Pull Request for the first time in my life
What is a dog? Volume of GET request and query parameters
What is a dog? Django--Get Name and Date from URL Volume
[Python] What is a with statement?
[For self-learning] Go2 for the first time
See python for the first time
What is the python underscore (_) for?
Start Django for the first time
Impressions and memorandums when working with VS code for the first time
A useful note when using Python for the first time in a while
Qiita's first post (the reason for starting)
I tried tensorflow for the first time
MongoDB for the first time in Python
Let's try Linux for the first time
A simple workaround for bots to try to post tweets with the same content
Turn multiple lists with a for statement at the same time in Python
What is a dog? Django--Create a custom user model
I tried using scrapy for the first time
How to use MkDocs for the first time
Make a histogram for the time being (matplotlib)
Use logger with Python for the time being
[Note] Deploying Azure Functions for the first time
I played with Floydhub for the time being
I tried python programming for the first time.
It's a Mac. What is the Linux command Linux?
What is a dog? Django--Create a custom user model 2
virtualenv For the time being, this is all!
Try posting to Qiita for the first time
I want to get started with the Linux kernel, what is the list head structure?
I don't know what HEIC is. But for the time being, let's use PNG!
I want to create a lunch database [EP1] Django study for the first time
I want to create a lunch database [EP1-4] Django study for the first time