xuepangzi

新建django项目NewCenter

1.开发环境

1.系统:Windows7

2.开发工具:pycharm

3.数据库:MySQL5.7

还是得选择5.7,选择8..0版本的,在往Ubuntu上部署项目的时候,真的坑得要死。

MySQL5.7.25.0下载地址:https://dev.mysql.com/downloads/file/?id=482771

如果之前在电脑里安装了MySQL8.0版本,需要先卸载干净了以后再安装MySQL5.7

卸载MySQL8.0教程:https://jingyan.baidu.com/article/ca41422f0d42701eae99edb2.html

安装MySQL5.7最省心的办法,就是一直下一步,所有的都选择默认设置就可以了。

4.Navicat

5.Xshell6

6.FileZilla

7.python3.6

电脑里安装的是python3.7.2,但是服务器上默认安装的python3目前只到3.6,所以要先卸载电脑里的python3.7,改安装python3..6

如何彻底卸载python的网址:https://jingyan.baidu.com/article/4dc408487d1f11c8d946f1b1.html

python下载地址:

https://www.python.org/downloads/windows/

 

 2.新建django项目NewCenter

1.新建

 

 2.新建app:trade、goods、user_operations

python manage.py startapp goods
python manage.py startapp trade
python manage.py startapp user_operations

 

3.新建目录

新建目录apps存放app

新建extra_apps存放xadmin和DjangoUeditor

新建static目录存放静态文件

新建media目录存放媒体文件

Mark一下apps和extra_app

 

 4.在settings.py中配置

1.配置目录路径

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\'))

 

 2.配置静态目录和媒体目录

STATIC_URL = \'/static/\'
# STATIC_ROOT = os.path.join(BASE_DIR, \'static\')
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, \'static\'),
)


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

 

 

\'django.template.context_processors.media\'

 

 3.注册

    \'goods.apps.GoodsConfig\',
    \'trade.apps.TradeConfig\',
    \'user_operations.apps.UserOperationsConfig\',
    \'DjangoUeditor\',
    \'xadmin\',
    \'crispy_forms\'

 

5.在NewCenter/NewCenter/urls.py中配置媒体文件路径

from django.contrib import admin
from django.urls import path,include
from django.views.static import serve
from NewCenter.settings import MEDIA_ROOT

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

3.新建数据库NewCenter

1.新建数据库NewCenter

打开mysql终端

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

2.连接数据库

1.在settings.py中:

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

2.安装PyMySQL:

pip install PyMYSQL

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

import pymysql
pymysql.install_as_MySQLdb()

4.在终端执行数据更新命令,生成数据表

python manage.py makemigrations
python manage.py migrate

4.在pycharm直接管理数据库

1.打开Database

 

 2.填入信息

jdbc:mysql://localhost:3306/NewCenter?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

 

 

 

 

5.安装djangorestframework及其依赖包markdown、django-filter

1.安装

pip install djangorestframework markdown django-filter

安装 pillow包(做图片处理的)

pip install pillow

2.注册

在settings.py中:

\'rest_framework\'

 

 6.在后端解决跨域

1.安装模块

pip install django-cors-headers

2.配置

INSTALLED_APPS = [
#……
    \'corsheaders\'
]

 

 

MIDDLEWARE = [
    \'corsheaders.middleware.CorsMiddleware\',#注意要放到前面
#……
]

 

 

 添加代码:

#跨域增加忽略
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
#CORS_ORIGIN_WHITELIST = (
#    \'*\'
#)

CORS_ALLOW_METHODS = (
    \'DELETE\',
    \'GET\',
    \'OPTIONS\',
    \'PATCH\',
    \'POST\',
    \'PUT\',
    \'VIEW\',
)

CORS_ALLOW_HEADERS = (
    \'XMLHttpRequest\',
    \'X_FILENAME\',
    \'accept-encoding\',
    \'authorization\',
    \'content-type\',
    \'dnt\',
    \'origin\',
    \'user-agent\',
    \'x-csrftoken\',
    \'x-requested-with\',
    \'Pragma\',
)

 

分类:

技术点:

相关文章: