This is the 14th day of my participation in the More text Challenge. For more details, see more text Challenge

If ❤️ my article is helpful, welcome to like, follow. This is the greatest encouragement for me to continue my technical creation. More in this series on my blog

Django Tutorial

The installation

pip install django -i pypi.tuna.tsinghua.edu.cn/simple/

  1. Files that generate environment dependenciespip freeze > requirements.txt

2. Install dependencies using requirements. TXT PIP install -r requirements. TXT

Starting a Django project

Django-admin basic command

Check that django-admin is installed

Startproject Create a project startApp Create an application check Verify project integrity RunServer Run the shell Django command line tool test Run unit test cases Makemignames Create model migration files Migrate: Execute the migration file created by the previous command dumpdata to export database data and loadData to import file data into the databaseCopy the code

Differences between Django projects and applications

  • Projects can run independently, applications can’t
  • Applications can manage their own models, views, templates, routes, and static files
  • Projects can contain multiple applications

startprojectCreate the Django-Intro project

django-admin startproject django-intro

Introduction to Project Catalog

Settings. py Project configuration file wsgi.py is a required file for WSGI applicationsCopy the code

Simple start project python./django_intro/manage.py runserver

startappCreating a Django Application

python ./django_intro/manage.py startapp blog

Application Directory

Admin.py background management app.py uses tests.py to write application test cases and urls.py to create and manage routesCopy the code

django hello world

Blog \views.py writes the helloWorld method

from django.http import HttpResponse

# Create your views here.
def hello_world(request) :
    return HttpResponse("hello world")
Copy the code

blog\urls.py

from django.urls import path, include
import blog.views

urlpatterns = [
    path('hello_world', blog.views.hello_world)
]
Copy the code

django_intro\urls.py

from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls')),]Copy the code

django_intro\settings.py

INSTALLED_APPS = [
    # myapp
    'blog.apps.BlogConfig',]Copy the code

python manage.py runserver

Model layer usage

Configure the database django_intro\settings.py

DATABASES = {
    'default': {
        # 'ENGINE': 'django.db.backends.sqlite3',
        # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'ENGINE': 'django.db.backends.postgresql_psycopg2'.'NAME': 'postgres'.# Database name
        'USER': 'postgres'.# username
        "PASSWORD" : 'postgres'.# own password
        "HOST":'127.0.0.1'.'PORT':5432,}}Copy the code

Define the model

Edit blog \ models. Py

from django.db import models

# Create your models here.
class Article(models.Model) :
    article_id = models.AutoField(primary_key=True)
    title = models.TextField()
    brief_content = models.TextField()
    content = models.TextField()
    publish_date = models.DateTimeField(auto_now=True)
Copy the code

Model of migration

python manage.py makemigrations

Migrations for 'blog':
  blog\migrations\0001_initial.py
    - Create model Article
Copy the code

Synchronize model to database

python manage.py migrate