Django learning framework to build (detailed)

Insert picture description here

Preface

There are two versions of the compiler pycharm: community version and professional version

For the professional version, Django can be created directly in the create project in the upper left corner of the file
Insert picture description here

For the community version, Django is not in the create project, we can create Django through the terminal

Prerequisite: Environment configuration

python3.7 (with Python environment)
Django 3.1.3 (with Django environment) has been installed in the settings

Insert picture description here

The terminal builds the basic framework of Django

-Build a Django project

-Enter the terminal

In the specified document, open the powershell windowInsert picture description here
Insert picture description here

-Create a Django project

Here is a demonstration with powershell
(you can also enter the preset folder in the pycharm terminal) to
execute the project creation command

django-amdin startproject mySite

Insert picture description here

At this point, a mySite folder is generated under the django directory
Insert picture description here

-View project structure

Continue to view the frame structure in the terminal

 tree /f mySite

Insert picture description here
The frame form is


mySite/
│  manage.py
│
└─mySite
        asgi.py
        settings.py
        urls.py
        wsgi.py
        __init__.py

-Build Django functional area

Enter the mySite project and create a functional area

cd mySite
django-admin startapp myApp

Insert picture description here
At this point, the created myApp appears in mySite
Insert picture description here

-View the overall structure

cd..
tree /f mySite

Insert picture description here
At this point, the basic structure of Django is in sight

mySite/
│  manage.py
│
├─myApp
│  │  admin.py
│  │  apps.py
│  │  models.py
│  │  tests.py
│  │  views.py
│  │  __init__.py
│  │
│  └─migrations
│          __init__.py
│
└─mySite
        asgi.py
        settings.py
        urls.py
        wsgi.py
        __init__.py

-Configuration file parameters

--Settings file configuration

Open the pycharm compiler and add the myApp file address in the settings in mySite
Insert picture description here

--Urls file configuration

Add index interface

from myApp import views
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index),
]

--Views file configuration

Feel free to design a paragraph. For example: hi, this is a funny stuff!

from django.http import HttpResponse

def index(request):
    return HttpResponse('hi, this is a funny stuff!')

Enter mySite in the terminal and run manage

cd mySite
python manage.py runserver

Show address after carriage return
Insert picture description here
Insert picture description here

Enter the display result through the browser

Enter http://127.0.0.1:8000/ in the browser

Insert picture description here
At this point, the terminal displays 200 and the access is successful.
Insert picture description here
Finally, if you want to end the operation, you can exit in the terminal through Ctrl+Break~


Write at the end

If my article is helpful to you, please like and follow!
If you are confused about my article, please leave a message!

Down to earth, there will be a bright future!

Guess you like

Origin blog.csdn.net/JasonZ227/article/details/112173031