Django learning.chapter1

Create project

django-admin startproject 项目名

Create app

python manage.py startapp app名

Start the Django browser and enter the URL 127.0.0.1:8000. The default port number is 8000

python manage.py runserver

Modify Django's default port

python manage.py runserver 端口号

Modify ip address

python manage.py runserver ip地址

setting configuration

Django static file configuration

STATIC_URL = '/static/'
STATICFILES_DIRS=[
    os.path.join(BASE_DIR,'static')
]

templates configuration

TEMPLATES = [
    {
    
    
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
    
    
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Add the app to the project

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'yingmo'#app名称
]

models

Insert picture description here

python manage.py makemigrations

Database migration command

D:\djngo project\momo>python manage.py makemigrations
Migrations for 'yingmo':
  yingmo\migrations\0001_initial.py
    - Create model user

D:\djngo project\momo>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, yingmo
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK
  Applying yingmo.0001_initial... OK

D:\djngo project\momo>

Use mysql database

1. Modify the database under settings

import pymsql
pymysql.install_asMySQLdb()

DATABASES = {
    
    
    'default': {
    
    
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '数据库名',
        'HOST':'127.0.0.1',
        'PORT':3306,
        'USER':'root',
        'PASSWORD':'root'
    }
}
import pymysql
pymysql.install_as_MySQLdb()

Usually written in

Insert picture description here

2. Write model in models.py under APP

Insert picture description here

3. Execute the database migration command

python manage.py makemigrations
python manage.py migrate

Same as SQLLITE

4. An error occurred during use

Insert picture description here

This is caused by the inconsistency of mysql and Django versions. Previously, Django3.1 MySQL5.0 was used.

At this time, it can be solved using Django2.0 and mysql8.0 versions

Guess you like

Origin blog.csdn.net/qq_44862120/article/details/115025450