美文网首页
django项目的配置步骤详解

django项目的配置步骤详解

作者: joyitsai | 来源:发表于2018-11-28 16:34 被阅读0次

1. 配置django项目

(1) 在pycharm中打开通过django-admin startproject myWeb所创建的django项目
(2) 会看到通过python manage.py startapp myBlog所创建的应用
(3) 创建模板文件夹templates和静态文件夹statics
(4) 创建数据模型,在models.py中进行编写:

from django.db import models

# Create your models here.

# define a SQL model to build a database
class UserInfo(models.Model):
    username = models.CharField(max_length=64)
    password = models.CharField(max_length=128)

在admin.py中向后台注册刚刚创建好的数据模型,这样在登录127.0.0.1/admin/时,才可查看到模型对应的数据信息:

from django.contrib import admin
from models import UserInfo

# Register your models here.
admin.site.register(UserInfo)

(5) 在项目的配置文件settings.py中安装APP:

图1.1: settings.py中安装刚刚创建的应用
(6) 在settings.py中配置模板文件路径:
图1.2:settings.py中设置模板文件的路径
(7) 在settings.py中进行数据库配置:
django默认的数据库是sqlite3:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

除了sqlite,我们常用的还是mysql,下面是mysql的配置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbName',
        'HOST': 'localhost',
        'USER': 'root',
        'PASSWORD': 'your_password',
        'PORT': '3306' # default port for connecting mysql
    }
}

在配置mysql数据库时,需要声明要连接的mysql数据库名NAME,主机名HOST,登录用户USER,登录密码PASSWORD,连接端口PORT
(8) 在settings.py中配置时区和语言:

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

# 配置时区为上海,语言为中文
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = False

(9) 在settings.py中配置静态文件路径:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIR = (
    os.path.join(BASE_DIR, "static").replace("\\", "/"),
)
STATIC_ROOT = os.path.join(BASE_DIR, "static/ckeditor").replace("\\", "/")

当运行python manage.py collectstatic时,STATIC_ROOT会将STATICFILES_DIRS中所有文件夹的文件,以及各个APP中的static中的文件都复制到static/ckeditor目录中,为了在进行apache等部署时更方便。
(9) 在settings.py中配置媒体文件:

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "static").replace("\\", "/")

当在models.FileField或models.ImageField中上传文件或上传图像时,参数中有一个upload_to='images',指定我们图像上传之后存放的文件夹名,此时要跟媒体文件配置结合使用,最终图像存放的目录是/BASE_DIR/static/images,其中BASE_DIR/static/由MEDIA_ROOT指定,/images/upload_to='images'指定。
(10) 在settings.py中配置ckeditor:
django-ckeditor是一个富文本编辑器,配置ckeditor后,我们就能在admin后台通过ckeditor进行对文本的编辑了,方便实用,但django-ckeditor==5.4.0依赖于pillow==5.1.0(python的一个图像处理库),注意版本兼容,ckeditor和pillow的版本需要特别注意:

CKEDITOR_UPLOAD_PATH = "uploads"
CKEDITOR_IMAGE_BACKEND = "pillow"

通过pip install ckeditor==5.4.0pip install pillow==5.1.0进行安装
(11) ckeditor的url配置:

urlpatterns = [
   ...,
   url(r'^ckeditor/', include("ckeditor_uploader.urls")),
]

(12) 数据同步与收集静态文件:

  • 数据模型语法校验:python manage.py validate
  • 数据模型同步成SQL语句:python manage.py makemigrations
  • 数据同步到数据库: python manage.py syncdb
  • 收集静态文件,将所有app中的static下的静态文件收集到static/ckeditor/目录下:python manage.py collectstatic

相关文章

网友评论

      本文标题:django项目的配置步骤详解

      本文链接:https://www.haomeiwen.com/subject/tndoqqtx.html