Djangolearning.chapter1

プロジェクトを作成する

django-admin startproject 项目名

アプリを作成する

python manage.py startapp app名

Djangoブラウザーを起動し、URL 127.0.0.1:8000を入力します。デフォルトのポート番号は8000です。

python manage.py runserver

Djangoのデフォルトポートを変更する

python manage.py runserver 端口号

IPアドレスを変更する

python manage.py runserver ip地址

設定設定

Django静的ファイル構成

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

テンプレート構成

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',
            ],
        },
    },
]

アプリをプロジェクトに追加します

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

モデル

ここに画像の説明を挿入します

python manage.py makemigrations

データベース移行コマンド

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>

mysqlデータベースを使用する

1.設定でデータベースを変更します

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()

通常はで書かれています

ここに画像の説明を挿入します

2.APPの下のmodels.pyにモデルを記述します

ここに画像の説明を挿入します

3.データベース移行コマンドを実行します

python manage.py makemigrations
python manage.py migrate

SQLLITEと同じ

4.使用中にエラーが発生しました

ここに画像の説明を挿入します

これは、mysqlとDjangoのバージョンの不一致が原因です。以前は、Django3.1MySQL5.0が使用されていました。

現時点では、Django2.0およびmysql8.0バージョンを使用して解決できます

おすすめ

転載: blog.csdn.net/qq_44862120/article/details/115025450