Contents
1.3. 用VS_Code编辑器进行开发¶
通过快捷键Ctrl+Shift+X打开扩展安装界面,安装需要的插件。
1.3.1. 1. 设置中文界面¶
安装chinese插件
1.3.2. 2.安装Python插件¶
安装python插件
Python Snippets
Pylance
run code
Bracket Pair Colorizer
1.3.3. 3.安装Django插件¶
安装如下插件
Django
Django Template
Django Snippets
Django Samples
HTML Snippets
HTML CSS Support
vs code一些常用的设置
https://zhuanlan.zhihu.com/p/345806901
{
"workbench.colorTheme": "Palenight Theme",
"files.associations": {
"*.html": "html"
},
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
"editor.fontSize": 18,
"editor.mouseWheelZoom": true,
"outline.showVariables": true,
"outline.showClasses": true,
"outline.showFunctions": true,
"outline.showMethods": true,
"python.linting.enabled": true,
"editor.formatOnSave": true,
"editor.formatOnType": true,
"python.autoComplete.addBrackets": true,
"python.autoComplete.extraPaths": [],
"python.analysis.extraPaths": [],
"editor.rulers": [
80,
],
"workbench.colorCustomizations": {
"editorRuler.foreground": "#ff4081"
},
"python.analysis.completeFunctionParens": true,
}
1.3.4. 4.开发第一个django应用¶
1. 创建应用¶
$ (env_pytest) E:\python_project>django-admin startproject myshop
$ (env_pytest) E:\python_project\myshop>python manage.py startapp app1
使用VS Code编辑器打开myshop项目。
创建应用后进行注册
打开myshop/settings.py文件
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
]
2. 处理控制器¶
1.处理视图的动态逻辑
app1/view.py
from math import remainder
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return render(request,'1/index.html')
2.处理URL请求路径
myshop/urls.py
from django.contrib import admin
from django.urls import path,include
from app1 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('index',views.index)
]
3.处理模板¶
1.创建模板目录和模板文件
创建templates/1/index.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div style="color: red; font-size: 24px;">你好 Dajngo !</div>
</body>
</html>
2.配置全局设置文件settings.py
myshop/settings.py
import os
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',
],
},
},
]
1.3.5. 5. 运行应用¶
通过命令启动项目
$ (env_pytest) E:\python_project\myshop>python manage.py runserver
或者
$ (env_pytest) E:\python_project\myshop>python manage.py runserver 0.0.0.0:8080
访问界面即可 http://127.0.0.1:8000/index/