程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[meeting Django] - (I) creating a project

編輯:Python

to encounter Django - Catalog

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


Preface

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 poetry Manage the project environment .
relevant poetry Installation 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

One 、 Environmental statement

  • development environment :MacbookPro M1
  • development tool :PyCharm
  • Python edition :3.9.x
  • Django edition :3.2.x
  • A virtual environment & Package management tools :poetry

Two 、 Create project

2.1 Installation dependency

poetry add [email protected]^3.2.13

2.2 see Django edition

python -m django --version
# or
django-admin --version

2.3 Django Version and Python Version comparison table

Django edition Python edition 2.23.5、3.6、3.7、3.8( stay 2.2.8 Add )、3.9( stay 2.2.17 Add )3.03.6、3.7、3.8、3.9( stay 3.0.11 Add )3.13.6、3.7、3.8、3.9( stay 3.1.3 Add )3.23.6、3.7、3.8、3.9、3.10( stay 3.2.9 Add )4.03.8、3.9、3.10

2.4 establish 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 :

  • External root , It's a container for projects . Its name is right Django It doesn't matter to me , You can rename it as you like .
  • manage.py: Command execution procedure , In various ways with this Django Project interaction .
  • mysite/settings.pyDjango Project settings / To configure .
  • mysite/urls.pyDjango Project URL Statement .
  • mysite/asgi.pyASGI Web Server portal .
  • mysite/wsgi.pyWSGI Web Server portal .

Be careful : When you create a project , Naming needs to be avoided python Built in packages and Django Component name

2.5 Open the simple server for development

# 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 return 404.

3、 ... and 、 Create an (app)

3.1 establish polls application

# stay manage.py Open the terminal under the same directory
python manage.py startapp polls

3.2 Write the first view

# 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 :route and view, Two optional parameters :kwargs and name.

  • 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 .

Four 、【PyCharm Use tips 】

Use PyCharm Tools for running Django project

  • Enable Django Support :Enable Django Support
  • menu bar - PyCharm - preference
    • If it is Windows or Linux, menu bar - File - Settings
  • Languages & Frameworks - Django

  • Edit and run the script

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.


summary

This article briefly introduced creating Django Project and create Django app.

And the use of PyCharm Run quickly Django project .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved