django basics (1)

1. What is Django?

Django is an open source web application framework written in Python, the code is open source.
This system adopts the framework mode of MVC, also known as MTV mode.
MVC: M (model/model layer) V (view/view layer) c (controller/control layer),
MTV: M (model/model layer), V (view/processing business logic, similar to controller).

2. Install virtualenv

Virtualenv usage scenario: When development members are responsible for multiple projects and there are many gaps in the libraries installed in each project, a virtual environment is used to isolate the environment of each project.

  1. Check python version (and go to its command line)
    python
  2. Enter pip3 on the command line to check whether the pip module is successfully installed (the pip3 module is responsible for the download of python3). When the following result is returned, the installation is successful
    pip3
    write picture description here
  3. Install the virtual environment
    pip3 install virtualenv
  4. Check whether the virtualenv is installed successfully . The
    virtualenv
    outputs the following information to indicate that the installation is successful!
    write picture description here
  5. Create a file in any path to save the django environment (the purpose of creating a folder is to facilitate the management of the running environment)
  6. Enter the above new folder path, and run the following command to install the env environment
    virtualenv –no-site-packages filename #Create a new virtual environment
  7. Enter the script path and start the django environment
    activate #Enter the virtual environment, deactivate exit the virtual environment
    pip freeze #Output the local package environment to the file

3. Install Django

  1. install pymysql
    pip install pymysql
  2. Install djangoV1.11
    pip install django==1.11
    1.11 indicates the version number information. When the version number is not filled in, the latest version is defaulted.
    write picture description here
  3. Create a workspace folder in any path and run the following command to create a project
    django-admin startproject project name
  4. Enter the helloworld path and run the following command to start the ajangp project

    python manage.py runserver ip: port number
    You can also just run python manage.py runserver without writing the following address and port number. The following information will appear after successful operation.
    write picture description here

  5. After the above services are started, you will see a URL 127.0.0.1:8000, open the browser, if the djiango environment is successfully built, the page will give corresponding prompts.

write picture description here

PS: You can modify the setting.py file in the environment to modify the value of LANGUAGE_CODE and set the language to Chinese.

write picture description here

4. Create a new project


  1. Run cmd and enter the disk cd ../../../ where the django environment is located

  2. View existing file information
    dir

  3. Start django service
    cd env\est\Script
    activate

  4. Enter the workspace, create a project name
    django-admin startproject project name

  5. After the above project folder is created, some related files will be produced in it. Now go to the folder and create a new project
    python manage.py startapp project name

  6. At this point, open pycharm to edit the project. For the specific code, please refer to the 2018.4.24 project folder in git

  7. Open the C:\workspace\helloworld file in pycharm, and enter the python interpreter Project
    Interpreter, enter add local

write picture description here
write picture description here

write picture description here

8. Select existing environment, and add the path where python.exe in django environment setup (1) is located at the interpreter
write picture description here

  1. Enter run-debug-edit
    configure, change the manage name to hello, the parameters to runserver, add the port number you want to set, and save the settings (the purpose of configuring Debug parameters: for when the method in your views is wrong, You can use debug to find the wrong place for debugging. If you do not use the debug model, the webpage will return the corresponding non-detailed error after the error. The specific debugging still has to be realized through debug. If Debug is configured, the webpage will report an error. Don't write a print after every sentence to see which variable is wrong or is a null pointer.)

write picture description here
write picture description here
write picture description here

  1. Run in the pycharm console: python manage.py startapp
    app, an app's python folder will appear under the helloworld folder on the upper left (if you can't find Terminal, you can click the small square in the lower left corner to open Terminal)

    init_.py : Initialize
    admin.py: Manage background registration model
    apps.py: Need to use when registering app in settings.py. It is generally not recommended to use
    from app.apps import AppConfig
    AppConfig.name
    models.py: where the model is
    written views.py: where the business logic is written

  2. Add the following parameters under INSTALLED_APPS in helloworld/setting

write picture description here

Five, MVC design pattern

MVC is a software development method that separates the code definition and data access methods (model) from request logic (controller) and user interface (view).

models.py: This file mainly uses a python class to describe the data table. called a model. Using this class, you can create, retrieve, update, delete records in the database with simple python code without writing one SQL statement after another.

views.py: This file contains business logic.

html template file: describes the page design, using a template language with basic logic declarations, such as {% for book in book_list %}.

6. urls.py

This document indicates what kind of URL calls what kind of view.
The code of appname/urls.py is as follows:

from django.conf.urls import url
from app import views

urlpatterns = [
    url(r'hello/', views.first_hello),
]

7. hello/urls.py

from django.contrib import admin
from app import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'hello/', views.first_hello),
]

8. app/views.py

from django.http import HttpResponse
from django.shortcuts import render

def first_hello(request):
return HttpResponse('hello world')

Run the hello file (the original manage), open the URL http://127.0.0.1:8080/hello , you can see the page output 'hello world!'

10. Run the following command in Terminal to create a stu python project

python manage.py startapp stu

11. Enter the following code in helloworld/urls.py

from django.conf.urls import url, include
from django.contrib import admin
from app import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'app/', include('app.urls')),
    url(r'stu/', include('stu.urls'))
    # url(r'hello/', views.first_hello),
    # url(r'hellogirl/', views.girl_hello)
]

stu/modes.py:
from django.db import models

class Student(models.Model):
    name = models.CharField(max_length=20)

sex = models.BooleanField()

class Meta:
    db_table = 'stu'

stu/apps.py:
from django.apps import AppConfig
class StuConfig(AppConfig):
name = ‘stu’

stu/urls.py:
from django.conf.urls import url

from stu import views

urlpatterns = [
    url(r'addstu/', views.addStudent),
]
stu/views.py:
from django.http import HttpResponse
from django.shortcuts import render
from stu.models import Student

def addStudent(request):
    print(request)

    return HttpResponse('添加成功')
Run the hello.py file, enter the path http://127.0.0.1:8080/stu/addstu/ , the page is successfully opened, indicating that the above configuration is correct

13. Open Navicat and create a database, either as follows or through the mysql statement

write picture description here

14. Enter the following parameters in helloworld/setting to connect to the database

write picture description here

15. After running the hello.py file correctly, run the following command in Terminal to migrate the database

python manage.py makemigrations
python manage.py migrate
Refresh the database firsthello and you will see that the table was successfully created

16. Enter the following code in stu/views

from django.http import HttpResponse
from django.shortcuts import render
from stu.models import Student

def addStudent(request):
    print(request)
    stu = Student()
    stu.name = '张三'
    stu.sex = 1
    stu.save()

return HttpResponse('添加成功')

17. Run the hello.py file, and enter http://127.0.0.1:8080/stu/addstu/ , check the data table stu, and you will find that a message has been added. (Every time the page http://127.0.0.1:8080/stu/addstu/ is refreshed , a row of records will be added to the data table stu)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325492066&siteId=291194637