Django study notes (3)-Django3.0 document study (1)

I recently used the book "Python Programming from Entry to Practice" to study the Django framework, but because the Django version used in the original book is 1.11, and the current version (the version I use) is 3.0.2, some of which Few differences, so I decided to read the official document first

Project file structure

By django-admin startproject mysitecreating, you can see the following file structure:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py

The external mysite/rootdirectory is a container for the project. Its name is irrelevant to Django; it can be renamed to any name.
manage.py: A command line utility that allows you to interact with this Django project in various ways.
The internal mysite/directory is the actual Python package of the project. Its name is the name of the Python package used to import any of the contents.
mysite/ __init__.py: An empty file telling Python that this directory should be treated as a Python package.
mysite/settings.py: Settings / configuration of this Django project. Django settings will tell you all about how settings work.
mysiteurls.py: URL declaration for this Django project; "directory" of sites supported by Django.
mysite/asgi.py: An entry point for asgi-compatible web servers to provide services for your projects.
mysite/wsgi.py: A web server compatible with wsgi is the entry point to provide services for your project.

App file structure

By python manage.py startapp pollscreating, you can see the following file structure:

polls/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py

You need to create urls.py in it, the file structure is as follows:

polls/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    urls.py
    views.py

urls.py

First look at urls.py in the app path:

rom django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

The path () function in urlpatterns

The path () function passes 4 parameters, two are required: route and view, and two are optional: kwargs and name.

Parameter route

route is a string containing a URL pattern. When processing a request, Django starts with the first pattern in urlpatterns, and then down the list, comparing the requested URL with each pattern until it finds a matching pattern.

The mode does not search for GET and POST parameters, nor does it search for domain names. For example, in a request for https://www.example.com/myapp/, URLconf will look for myapp /. At https://www.example.com/myapp/? In the request of page = 3, URLconf will also look for myapp /.

Parameter view

When Django finds a matching pattern, it calls the specified view function with the HttpRequest object as the first parameter and any "captured" value in the route as the keyword parameter.

Parameter kwargs

You can pass any keyword parameter to the target view in the dictionary.

Parameter name

Naming a URL allows you to refer to it explicitly from elsewhere in Django, especially within templates. This powerful feature allows you to make global changes to the URL pattern of a project while only touching a single file.

And for urls.py in the project:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

The include () function allows to reference other URLconf. Whenever Django encounters include (), it will cut off any part of the URL that matches that point, and send the remaining string to the included URLconf for further processing.

When including other URL patterns, you should always use include (). admin.site.url is the only exception.

settings.py

build database

The code in settings.py is as follows:

# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

You can see that by default, SQLite is configured.

However, when starting the first real project, you may want to use a more scalable database, such as PostgreSQL, to avoid the trouble caused by database switching.

If you want to use another database, you need to install the corresponding database binding and change defaultthe following keys in the database " " item to match the database connection settings:

ENGINE– Can be " django.db.backends.sqlite3", " django.db.backends.postgresql", " django.db.backends.mysql" or " django.db.backends.oracle". Other backends can also be used.

NAME– The name of the database. If you are using SQLite, the database will be a file on your computer; in this case, NAMEit should be the full absolute path of the file, including the file name. The default value os.path.join(BASE_DIR,'db.sqlite3')stores the file in the project directory.

If you do not use SQLite as a database, you must add other settings, such as user, password, and host.

INSTALLED_APPS

Here are saved the names of all Django applications activated in this Django instance. Applications can be used in multiple projects, and they can be packaged and distributed for others to use in their projects.

By default, INSTALLED_APPS contains the following applications, all of which are provided with Django:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    
    #myapps
    
]

django.contrib.admin——Manage the site. You will use it soon.
django.contrib.auth-Identity verification system.
content types——Framework of content type.
django.contrib.sessions——Conversational framework.
django.contrib.messages-A message framework.
django.contrib.static files——Framework for managing static files.

By default, these applications are included to facilitate use in common situations.

However, some of these applications use at least one database table, so before using these tables, we need to create these tables in the database. To do this, run the following command:

python manage.py migrate

migrateCommand to view the INSTALLED_APPSsettings and create any necessary database tables based on the database settings in the mysite / settings.py file and the database migration included with the application.

Published 28 original articles · won praise 2 · Views 3259

Guess you like

Origin blog.csdn.net/Maestro_T/article/details/104028652