current position:Home>Django web development 03. Install Django module and create App, operating and closing

Django web development 03. Install Django module and create App, operating and closing

2022-08-06 10:32:27Jay_fearless

1. Enter the command in the Pycharm console

pip install django

After that, check for the following two files:
insert image description here
If there are any, the installation is successful.

2. Create a project

1. Determine which directory the project is placed in and enter that folder

2.cmd enters the console

3. Console input execute the following command form

django-admin.exe installation path startproject project name

Example:
C:\Users\13529\PycharmProjects\test01\files>C:\Users\13529\PycharmProjects\test01\venv\Scripts\django-admin.exe startproject mysite
After that, check whether the directory file already has a django project package:

insert image description here
Initial project structure:

3.Django App

A project can have multiple apps, and each app can correspond to a function:

insert image description here

First go to our project folder mysite in the console in Pycharm, and then enter the following command to create app01:

python manage.py startapp app01

Insert image description here
The created App project structure is basically as follows:
insert image description here

After that, we write a test page function index in views.py of app01:

views.py

from django.http import HttpResponsefrom django.shortcuts import render# Create your views here.def index(request):return HttpResponse("Welcome")
Then write a path(URL, function) in mysite's urls to form a mapping relationship:

urls.py

from django.urls import pathfrom app01 import viewsurlpatterns = [# path('admin/', admin.site.urls),# www.xxx.com/index -> functionpath('index', views.index),]

4. Django project startup and shutdown

First use the cd command to enter the project path in the console, and then run the following command in the Pycharm console:

python manage.py runserver

After the project is started, we test the following page jumps:

insert image description here

successfully entered the page index:

insert image description here

Press the Ctrl + C keys to close the project:

insert image description here

copyright notice
author[Jay_fearless],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/218/202208061025005199.html

Random recommended