1.新建django项目newnet

1.新建项目newnet,新建app:users:

虚拟环境选个能用的

2.新建mysql数据库newnet:

打开mysql终端:

show databases; --查看所有数据库
create database newnet default character set utf8 collate utf8_general_ci; --创建数据库gg,因为创建数据表内有中文字段,所以要加default
show databases; --查看所有数据库

 3.连接数据库:

1.在settings.py中:

DATABASES = {
    ‘default‘: {
        ‘ENGINE‘: ‘django.db.backends.mysql‘,
        ‘NAME‘: ‘newnet‘,
        ‘USER‘:‘root‘,
        ‘PASSWORD‘:‘数据库密码‘,
        ‘HOST‘:‘127.0.0.1‘,
        "OPTIONS":{"init_command":"SET default_storage_engine=INNODB;"}#第三方登录功能必须加上
    }
}

 2.安装PyMYSQL:

pip install PyMYSQL

3.在newnet/newnet/__init__.py中加代码:

import pymysql
pymysql.install_as_MySQLdb()

4.新建app

1.新建app:good、trade、user_operation

python manage.py startapp good
python manage.py startapp trade
python manage.py startapp user_operation

2.在settings.py中配置app:

INSTALLED_APPS = [
    ‘django.contrib.admin‘,
    ‘django.contrib.auth‘,
    ‘django.contrib.contenttypes‘,
    ‘django.contrib.sessions‘,
    ‘django.contrib.messages‘,
    ‘django.contrib.staticfiles‘,
    ‘users.apps.UsersConfig‘,
    ‘good.apps.GoodConfig‘,
    ‘trade.apps.TradeConfig‘,
    ‘user_operation.apps.UserOperationConfig‘
]

5.新建目录 静态文件目录static,媒体文件目录media:

1.新建两个目录

1.新建django项目newnet

2.在settings中配置路径:

STATIC_URL = ‘/static/‘
STATIC_ROOT = os.path.join(BASE_DIR, ‘static‘)

MEDIA_URL=‘/media/‘
MEDIA_ROOT=os.path.join(BASE_DIR,‘media‘)

 3.在newnet/urls.py中配置媒体文件路径:

from django.contrib import admin
from django.urls import path
from django.views.static import serve
from newnet.settings import MEDIA_ROOT

urlpatterns = [
    path(‘admin/‘, admin.site.urls),
    path(‘media/<path:path>‘,serve,{‘document_root‘:MEDIA_ROOT}),

]

6.整理项目目录:

1.新建apps包用来存放我们新建的app,将users、good、trade、user_operation拖入。

2.新建extra_apps包,用来存放第三方包。

3.mark路径(apps和extra_apps都要mark)

3.settings中配置:

import sys
sys.path.insert(0,BASE_DIR)
sys.path.insert(0,os.path.join(BASE_DIR,‘apps‘))
sys.path.insert(0,os.path.join(BASE_DIR,‘extra_apps‘))

 4.导入第三方包xadmin和DjangoUeditor

相关推荐