[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 1-

Introduction

Nice to meet you, everyone. I'm going to publish a memorandum of the process of creating a voting (poll) application using Django. Since I am a beginner of Qiita, please understand that some parts may be difficult to read.

series

-[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-No. 0-

Start work

Let's follow the tutorial! URL↓

Creating your first Django app, part 1

Django version confirmation (confirmation of this series-No. 0-)

First, start the virtual environment. In the Django tutorial, it is installed in the real environment, but in this series, we are building a virtual environment.


C:\django\poll>pipenv shell
Launching subshell in virtual environment…
Microsoft Windows [Version 10.0.18362.1082]
(c) 2019 Microsoft Corporation. All rights reserved.
(poll-HcNSSqhc) C:\django\poll>

Check your Django version.


(poll-HcNSSqhc) C:\django\poll>python -m django --version
3.1.2

(poll-HcNSSqhc) C:\django\poll>

Project creation (confirmation of this series-No. 0-)

Since we have already created a project in "This series-No. 0-", I would like to take a look at the files that make up the project. image.png

In the Django tutorial, the project name is "mysite", but in this series it is "config". Please read it again.

Quoted below * 1

--The outer mysite / root directory is the project's container. That name doesn't matter to Django. You can change it to any name you like. --manage.py: A command line utility for doing various things with your Django project. See django-admin and manage.py in manage.py for more information. --The inner mysite / directory is the actual Python package for this project. This is the name of the Python package and the name you will use when importing (for example, import mysite.urls). --mysite / __ init__.py: An empty file to let Python know that this directory is a Python package. If you are new to Python, read more about packages in the official Python documentation. --mysite / settings.py: Django project configuration file. See Django Settings for how it works. --mysite / urls.py: The URL declaration for your Django project, the "table of contents" for your Django site. See URL Dispatcher for more information. --mysite / asgi.py: The entry point for the ASGI compatible web server that provides the project. See How to deploy with ASGI for more information. --mysite / wsgi.py: An entry point with a WSGI compatible web server for serving projects. See To deploy with WSGI for more information.

Development server (confirmation of this series-No. 0-)

Start the development server.


(poll-HcNSSqhc) C:\django\poll>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations 
for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
October 04, 2020 - 14:41:45
Django version 3.1.2, using settings 'config.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Open "http://172.0.0.1:8000/" in your browser and check the top page. Make sure you see "HTTP 200" in your terminal.


[04/Oct/2020 14:42:14] "GET / HTTP/1.1" 200 16351

Create a Polls application

Let's create a "polles" application.

(poll-HcNSSqhc) C:\django\poll>python manage.py startapp polls

At this time, check that the following polls files have been created. image.png

Create your first view

Edit the file as follows.

A view file that describes what is displayed on the screen. "Hello, world. You're at the polls index." Is displayed.

polls/views.py


from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse

def index(request):
    return HttpResponse( "Hello,world.You're at the polls index." )

A controller file that associates a URL to display "poll / polls / views.py". In the following, the index function in the current (poll / polls /) views.py is executed with the argument name ='index'.

polls/urls.py



from django.urls import path
from . import views

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

Front controller file. If "http \ / //172.0.0.1:8000/polls/" is accessed, polls / urls.py is returned. If "http \ / //172.0.0.1:8000/polls/" is accessed, admin / site / urls.py is returned. ← The management screen is displayed.

config/urls.py



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

urlpatterns = [
    path('polls/', include( 'polls.urls' )),
    path('admin/', admin.site.urls)
]

Start the development server

(poll-HcNSSqhc) C:\django\poll>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
October 04, 2020 - 17:05:53
Django version 3.1.2, using settings 'config.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Open "http://172.0.0.1:8000/polls/" in your browser and check the top page. If "Hello, world. You're at the polls index." Is displayed, it is OK. image.png

That's all for today. Thank you very much.

Recommended Posts

[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 7-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 1-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 2-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 0-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 5-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 6-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 4-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 3-
Django python web framework
Deploy a Python 3.6 / Django / Postgres web app on Azure
(Python) Try to develop a web application using Django
CTF beginner tried to build a problem server (web) [Problem]
Web scraping beginner with python
[Django3] Display a web page in Django3 + WSL + Python virtual environment
A liberal arts engineer tried knocking 100 language processes in Python 02
A python beginner tried to intern at an IT company
A liberal arts engineer tried knocking 100 language processes in Python 01
A liberal arts engineer tried knocking 100 language processes in Python 00
If you know Python, you can make a web application with Django
[Python / Django] Create a web API that responds in JSON format
I tried web scraping with python.
Build a web application with Django
Python beginner tried 100 language processing knock 2015 (05 ~ 09)
web coder tried excel in Python
When a Python beginner tried using Bottle, it worked unexpectedly easily.
Python beginner tried 100 language processing knock 2015 (00 ~ 04)
A beginner of machine learning tried to predict Arima Kinen with python
An introduction to self-made Python web applications for a sluggish third-year web engineer
A note where a Python beginner got stuck
[Beginner] Python web scraping using Google Colaboratory
I have a question! (Python, django) Easy
Daemonize a Python web app with Supervisor
I tried a functional language with Python
[Python] A quick web application with Bottle!
I tried to make a Web API
Use Django from a local Python script
Run a Python web application with Docker
Let's make a web framework with Python! (1)
I tried benchmarking a web application framework
Let's make a web framework with Python! (2)
I made a WEB application with Django
A python beginner tried to intern at an IT company [Day 2 chatbot survey]
A python beginner tried to intern at an IT company [Day 1 development process]
[ES Lab] I tried to develop a WEB application with Python and Flask ②
I searched for the skills needed to become a web engineer in Python
2-2. Input for becoming a WEB engineer (Linux basics)
How to open a web browser from python
I tried web scraping using python and selenium
Python web framework Django vs Pyramid vs Flask December 2015
I tried playing a typing game in Python
Start a simple Python web server with Docker
[Python] Build a Django development environment with Docker
[Memo] I tried a pivot table in Python
[Python] Draw a Mickey Mouse with Turtle [Beginner]
Create a web map using Python and GDAL
Steps to develop a web application in Python
[Python] Web development preparation (building a virtual environment)
I tried reading a CSV file using Python
Programming beginner Python3 engineer certification basic exam record
Launch a web server with Python and Flask
Machine learning memo of a fledgling engineer Part 2