Part 1:【 to encounter Django】—— ( One ) Create project
️ Part 2:【 to encounter Django】—— ( Two ) Database configuration
️ Part 3:【 to encounter Django】—— ( 3、 ... and ) View
️ Part 4:【 to encounter Django】—— ( Four ) Forms and common views
️ Part 5:【 to encounter Django】—— ( 5、 ... and ) Perfect interface ( Customize interfaces and styles )
️ Part 6:【 to encounter Django】—— ( 6、 ... and ) User defined management interface
️ Part 7:【 to encounter Django】—— ( 7、 ... and ) automated testing
This series of articles , stay Django Under the basic template of the official document tutorial , Some improvements and deletions have been made ( Do not explain the relevant contents of the template ), Added some of my own insights .
I hope that after reading this series of articles , Yes Django Be able to have a clear understanding .
It's a long way to go , I will go up and down !
Django Official documents :https://www.djangoproject.com/
Learning process , Read more official documents , Can solve many problems
This tutorial USES
poetryManage the project environment .
relevantpoetryInstallation and use of , Please refer to 【Python - A virtual environment 】 The start of the project , Start by isolating the development environment - CoderChaos - Blog Garden (cnblogs.com)
Project address :https://github.com/CoderBerryRabbit/MeetDjango
MacbookPro M1PyCharm3.9.x3.2.xpoetrypoetry add [email protected]^3.2.13
Django edition python -m django --version
# or
django-admin --version
Django Version and Python Version comparison table Django edition Python edition Django project django-admin startproject mysite
explain :
django-admin startproject: The command to create a project mysite: Create the name of the project ( Customize ) see startproject Created Directory
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
Directory description :
manage.py: Command execution procedure , In various ways with this Django Project interaction .mysite/settings.py:Django Project settings / To configure .mysite/urls.py:Django Project URL Statement .mysite/asgi.py:ASGI Web Server portal .mysite/wsgi.py:WSGI Web Server portal . Be careful : When you create a project , Naming needs to be avoided python Built in packages and Django Component name
# stay manage.py Open the terminal under the same directory
python manage.py runserver
# After successful startup , There will be the following output
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
June 13, 2022 - 09:55:14
Django version 3.2.13, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
The server is running normally , Access... Through a browser :http://127.0.0.1:8000/.
Will see a “ Congratulations on ” page , There is a rocket launching .
Be careful : After adding other routes , If the view corresponding to the root route is not specified , Access the root route again
http://127.0.0.1:8000, Will return404.
polls application # stay manage.py Open the terminal under the same directory
python manage.py startapp polls
# polls/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
stay polls Create a new file in the directory urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
take polls/urls.py In the path , Add to root URLcon in .
stay mysite/urls.py in , introduce from django.urls import include.
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
Start development server , Access in browser :http://127.0.0.1:8000/polls/.
That is visible "Hello, world. You're at the polls index." , This is index Defined in the view .
function path() explain :
function
path()With four parameters , Two required parameters :routeandview, Two optional parameters :kwargsandname.
route: matching URL Principle of . When Django In response to a request , From urlpatterns The first item of , Match the items in the list in order , Until you find a match .view: When Django Find a matching criterion , This particular view function will be called , And introduce a HttpRequest Object as first argument , By “ Capture ” The parameter of is passed in the form of a key value parameter .kwargs: Any keyword parameter can be passed to the target view function as a dictionary .name: by URL The name , In the Django It's only quoted anywhere .PyCharm Use tips 】PyCharm Tools for running Django project Django Support :Enable Django Support




After finishing the above operation , You can use it directly PyCharm The tool runs directly Django project .
Instead of typing... On the command line every time python manage.py runserver.
This article briefly introduced creating Django Project and create Django app.
And the use of PyCharm Run quickly Django project .