DJango first class

Posted by mrsocks on Mon, 10 Jan 2022 20:12:55 +0100

  • Create virtual environment: mkvirtualenv -p python3 virtual environment name
  • Use virtual environment: workon (add the virtual environment name to enter the corresponding environment, and do not enter to list all virtual environments)
  • Exit virtual environment: deactivate
  • Delete virtual environment: rmvirtualenv virtual environment name, exit first and then delete
  • Installation kit: pip install + Kit
  • View the package pip list in the environment
  • Create django project: django admin startproject name
  • Create sub application: Python manager py startapp name
  • Register the method of installing the sub application and the configuration information file of the sub application apps Add Config class from. Py to installed_ In the apps list
    • Example: add the newly created Book sub application to the project, which can be installed in the_ Add 'book' to the apps list apps. BookConfig
settings.py Is the overall configuration file of the project.

urls.py It's part of the project URL Configuration file.
effect:(yes Django Directory of supported websites. Its essence is URL Mode and the URL The mapping table between view functions called by the schema. Tell in this way Django,For that URL Call that code. url The loading of starts from the configuration file
)
1.urlpatterns Is a fixed way of writing, and its value is a list
2.The path we enter in the browser will match urlpatterns Match each item in order
 If the matching is successful, it will be directed to the responding module,
If the match is unsuccessful, 404 is returned

3.urlpatterns The element in is url
4.agreement, ip And port, parameters do not participate in matching

Create a sub application urls.py
from django.conf import url
from book.views import index
url The first parameter in is: regular
 Second parameter function name
urlpatterns=[
url(r'^index/$',views.index)
]




wsgi.py Project and WSGI Compatible Web Server portal.

manage.py Is a project management file through which to manage projects.

 

 

 

django is a framework for python web development

web application

Frame:

  1. Building web applications

  2. You only need to care about the business logic code of the core of the web application

The essence of a web application is:

  1. Receive http request and obtain specific request parameters

  2. Process this request and complete the business logic

  3. Return response data

 

Software development mode:

mvc

m: Model model: encapsulate the access to the database, add, delete, modify and query

v: View view: encapsulates the results and generates the page display content

c: controller: control: receive requests, process business, interact with model and view, and return results

 

 

 

 

mvt:

m: Model model: encapsulate the access to the database, add, delete, modify and query

v: View view: it has the same functions as c in mvc. It receives requests, processes services, interacts with model and t, and returns results

t: Template: template: it has the same function as v in mvc. The user constructs the html to be returned

 

 

 

 

 

1. Do not operate the database

2. Operation database

 

Software preparation:

1.pycharm

2.linux (wsl virtual machine + wubantu)

 

windows

linux

macos

The operating system must be installed in a machine

Virtual machine: software (simulating a machine) - > CentOS Uban diagram

ECS (machine)

 

Virtual environment:

In the actual development process, go back and download various corresponding versions or frameworks according to the needs

Scenario:

Project A requires Framework version 1.0 and project B requires version 2.0

Python 2.0 for the project 7 environment needs to let him in Python 3 6 or above

 

Create project

Enter ubantu terminal
# View virtual environment version
virtualenv --version
# View which packages or frameworks are installed in the current virtual environment pip list # install django
pip install django==3.2 # establish django_demo Folder save django code mkdir django_demo # Enter folder cd django_demo # establish django Project, project name is demo django-admin startproject demo # function manage.py Start project python manage.py runserver django The default port number is 8000 visit: 127.0.0.1:8000

 

Sub application

admin. The PY file is related to the background management site configuration of the website.

apps. The PY file is used to configure the relevant information of the current sub application.

The migrations directory is used to store database migration history files.

models.py file saves the database model class to the user.

tests.py file is used to develop test cases and write unit tests.

views.py file is used to write the Web application view.

Project configuration file settings Py, installed_ The apps item saves the registered and installed sub applications in the project

 

 

 

 

django works in the Debug mode by default. If you add, modify or delete files, the server will restart automatically. Press ctrl+c to stop the server.

  

  • Run: Python manage Py runserver IP: Port

  • python manage.py runserver

##### 1.url introduction, features and concepts

##### 2 authoring views

##### 3. Use of include() method

##### 4. Steps of using template

##### 5. Render the template

##### 6. Use of render() method

##### 7. Introduction and use of template variables

##### 8. Introduction and use of filter

##### 9. Introduction to automatic escape

##### 10. Use of static files

 

str, which matches a non empty string other than the path separator (/), which is the default form
int, matches a positive integer, including 0.
slug, a string that matches letters, numbers, horizontal bars and underscores.
uuid, matching the formatted uuid, such as 075194d3-6885-417e-a8a8-6c931e272f00.
Path, which matches any non empty string, including the path separator

 

A project has a total URLs Py each app can also create its own URLs Py uses the include() function in the URLs. Of project Py file for registration

Main under project directory urls.py
from django.contrib import admin

from django.urls import path,include

from . import views
urlpatterns =[
path('admin /' , admin.site.urls),

path('book/ ,include('book.urls')),

]

  

Job:

Connect to ubantu remotely in the local pycharm, create a django project and access it.