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-
Let's follow the tutorial! URL↓
Creating your first Django app, part 1
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>
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.
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.
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
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.
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.
That's all for today. Thank you very much.
Recommended Posts