I'm working and there are a lot of things related to Django's libraries that I wanted to touch once
--Understanding the flow of application creation --Understand the libraries you can use
Creating your first Django app, part 1|Django documentation| Django
django-admin startproject mysite
Running the above code will create a project for django with the name mysite
.
The project structure is as follows.
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
--The guy who does various operations on the manage.py project --The inner mysite is a Python package --mysite / settings.py: Django project configuration file --mysite / urls.py: Django project URL declaration (like a controller?) --mysite / asgi.py: Entry point for the ASGI compatible web server that provides the project Entry point: The place where the execution of a program or subroutine starts when the program is executed (something like main.py).
--mysite / wsgi.py: Web server
If you execute the above code in the directory where manage.py is located, that is, the outer mysite, it will start up.
python manage.py runserver
If you want to change the server port, do as follows.
python manage.py runserver 8080
Execute the following command in the same hierarchy as manage.py
python manage.py startapp polls
A directory called polls is automatically generated. The contents are as follows.
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
The polls
part is optional. The start app is important.
Add urls.py and create urls.py and views.py.
polls/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
--Added urlconf in the list of urlpatterns.
--In django, when you encounter include (), it cuts up to the url that matches up to that point.
--Pass the rest of the string to the included URLconf for the next process.
For http: // polls / hogehoge /
http: // polls / (cut off to this point)
, hogehoge / (pass this to polls.urls)
Check the execution result with the following command
$ python manage.py runserver
Go to http: // localhost: 8000 / polls / and Success if "Hello, world. You`re at the polls index." Is displayed
Recommended Posts