Django Learning-Lecture 1 (Part 2) Introduction to Django Framework and Environment Setup

1. Introduction to Django Framework

Django also follows the MVC idea, but has its own noun called MVT

Django, pronounced [`dʒæŋɡəʊ], was born in the autumn of 2003, and the official version was released in 2005, developed by Simon and Andrian.

2. Choice of Django version and Python version

Reference address: https://docs.djangoproject.com/zh-hans/2.1/faq/

3. Django development principles

Rapid development and DRY principle. Do not repeat yourself. Do not repeat yourself.

4. Official Manual

Django's official website: https://www.djangoproject.com/
Django Book2.0 version of the Chinese document: http://djangobook.py3k.cn/2.0/chapter01/

5. Django project related knowledge

5.1 How to create a Django project

1. Use the command line

Through the command line, find the disk path where we want to put the project, enter the corresponding directory,
create the project: open the terminal, use the command: django-admin startproject [project name] For
example: django-admin startproject first_project.

2. Use pycharm
if pycharm is a professional version, you can use pycharm to install Django

5.2 Run Django project

    1. Via the command line: python  manage.py  runserver. This way you can visit your website locally, the default port number is 8000, so you can visit your website through http://127.0.0.1:8000/ in the browser. If you want to modify the port number, you can specify the port number when you run it, python  manage.py  runserver 9000 can be accessed through port 9000.
  • 2. Run the Django project through pycharm and set it in edit configurations

5.3 Project structure introduction

manage.py : The future interaction with the project is basically based on this file. Generally, enter python manage.py  [subcommand] in the terminal  . You can enter python  manage.py  help to see what can be done. Unless you know what you are doing, you should not edit this file under normal circumstances.

settings.py : The setting items of this project. All future project-related configurations are placed in this.

urls.py : This file is used to configure URL routing. For example, visiting http://127.0.0.1/news/ is visiting the news list page, and these things need to be completed in this file.

wsgi.py : The entry to the web server compatible with the WSGI protocol, which is needed during deployment, and generally does not need to be modified.

5.4 The relationship between project and app

App is an integral part of the django project. An app represents a module in the project, and all URL requests are handled by the app. For example, Douban, there are many modules such as books, movies, music, and the same city. From the perspective of django, the modules of books and movies are apps. Books and movies together form the Douban project. Therefore, there must be a concept here. The Django project is composed of many apps. One app can be used in other projects, and Django can also have different apps.

5.4.1 Creation of app

Enter the created project directory by command, and then execute the following command:
python manage.py startapp [app name]

5.4.2 Files in the app

the init .py Contents is a Python module
models.py written and relevant content database
views.py receives the request, processes the data and M and T interacting
file tests.py write test code (no need to be concerned about)
admin.py website Background management related

5.4.3 app registration

To establish the connection between the application and the project, the application needs to be registered.

Modify the INSTALLED_APPS configuration item in settings.py.

5.6 The first Django project experience

from django.http import HttpResponse
from book.views import book
from move.views import move

def index(request):
    return HttpResponse("首页")

def book(request):
    return HttpResponse("图书首页")

def move(request):
    return HttpResponse("电影首页")

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',index),
    path("book",book),
    path("move",move)
]

5.7 DEBUG mode

  • 1. Turn on the debug mode, then modify the code, and then press ctrl+s, then Django will automatically restart the project
  • 2. There is a problem with the code in the Django project, and the error message will be printed in the browser and console
  • 3. If the project is online, turn off the debug mode, otherwise there is a big security risk
  • 4. Turn off DEBUG mode, in the setting file, set DEBUG = False

Guess you like

Origin blog.csdn.net/scyllake/article/details/99721451