Python writes websites, Django writes websites

Create projects and apps

1. Install Django, use the command to install (do not set the version, default to the latest stable version)

pip install Django

set version install command

pip install Django==3.2

2.cmd Enter the directory where you want to create a new Django project and execute the command to create a new Django project

Django-admin startproject DjangoDome

 

Open the directory of DjangoDome

3. Start the site, use the command 

Python manage.py runserver

set port command 

Python manage.py runserver==8089

The browser opens localhost:8000, as shown below

 4. Then create a site in the DjangoDome directory and use the command

Django-admin startapp web

 

 open web

 Configure MySql

1. Modify DATABASES in setting.py and configure it as MySql

DATABASES = {
    'default': {
        # 'ENGINE': 'django.db.backends.sqlite3',
        # 'NAME': BASE_DIR / 'db.sqlite3',
        'ENGINE': 'django.db.backends.mysql',
        'HOST': 'localhost',
        'PORT': '3306',
        'USER': 'root',
        'Name':'jike',
        'PASSWORD': 'root'
    }
}

2. Then install mysqlClient, command:

pip3 install mysqlclient

3. Next install coreapi, command

pip install coreapi

 Python3 is installed with pip3

pip3 install coreapi

4. Then modify it in setting.py under the project

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework_swagger'
]

Backstage beautification

install simpleui

pip install django-simpleui

setting.py configures INSTALLED_APPS, which must be placed on the first line

INSTALLED_APPS = [
    'simpleui',  # <--美化后台
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'jike.apps.JikeConfig'
]

Static resource configuration

css, js, imag files, etc.

# 静态资源文件配置
STATIC_URL = 'static/'  # 静态资源的路由
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)  # 静态资源的路径配置

Dynamic resource allocation

Implement file upload and access uploaded files

Configure in setting.py

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

add route

from django.conf.urls.static import static
urlpatterns+=static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

OPTIONS at TEMPLATES

'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',
                'django.template.context_processors.media'  # 动态文件配置
            ],
        },

reference in the page

#这种写法保证了更改目录了也不影响文件访问
<img src="{
   
   { MEDIA_URL }}{
   
   { avatar }}"class="layui-nav-img">

 TinyMCE Rich Text Editor

1. Install TinyMCE

 pip install django-tinymce 

2. Add App in setting.py

INSTALLED_APPS = [
    #省略其他代码
    'tinymce'
    
]

3. Add the following configuration in setting.py

TINYMCE_DEFAULT_CONFIG = {
    'theme': 'silver',  # 主题
    'language': 'zh_CN',  # 语言
    'menubar': 'edit format',  # 菜单栏
    'plugins': 'lists,advlist bold underline alignleft aligncenter alignright fontselect fontsizeselect code image link table',
    'toolbar': 'bullist numlist bold underline alignleft aligncenter alignright fontselect fontsizeselect code image link table',
    'images_upload_url': 'upload',  # 图片上传处理视图
    'width':'600',  # 富文本编辑器的宽
    'height': '400',  # 富文本编辑器的高
}

4. Configure routing

re_path(r'^tinymce/', include('tinymce.urls')),  # tinymce路由
path('jike/tiny_upload/', jike.views.tiny_upload)  # 文件上传路由

5. Configure the editor file upload view

# 文件上传
@csrf_exempt  # 防止post的时候引发403
def tiny_upload(request):
    img_obj = request.FILES.get('file')
    from django.conf import settings
    fname = os.path.join(settings.MEDIA_ROOT, img_obj.name)

    with open(fname, "wb") as rfile:
        data = img_obj.file.read()
        rfile.write(data)
    return JsonResponse({
        "location": MEDIA_URL + img_obj.name
    })

IIS deployment

Instructional video: https://www.bilibili.com/video/BV1bT4y1M7L2?spm_id_from=333.337.search-card.all.click icon-default.png?t=M3C8https://www.bilibili.com/video/BV1bT4y1M7L2?spm_id_from=333.337.search-card. all.click

Install the virtual environment:

pip install virtualenv -i https://mirrors.aliyun.com/pypi/simple/
pip install virtualenvwrapper-win -i https://mirrors.aliyun.com/pypi/simple/

Create a virtual environment:

#创建djangodome
mkvirtualenv djangodome
#查看虚拟环境目录
workon
#切换到虚拟环境就
workon djangodome #djangodome虚拟目录

Copy the django project to wwwroot, enter the django project root directory in the virtual environment, and configure the environment

cd C:\wwwroot\jike.xiaoxingbobo.top\diangoProject

build dependency environment

pip freeze > ./requirements.txt
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/

install wfastcgi

pip install wfastcgi -i https://mirrors.aliyun.com/pypi/simple/

Guess you like

Origin blog.csdn.net/baidu_39105563/article/details/123225024
Recommended