Django项目及App创建及文件配置
1.创建虚拟环境
python
python -m venv myenv
2.激活虚拟环境
python
myenv\Scripts\activate
3.安装插件
python
pip install Django
4.创建项目
python
django-admin startproject myproject
5.创建App
python
python manage.py startapp app_name
6.配置
a.项目url.py文件中配置
python
# 包含App中的urls.py
from django.urls import path, include
path('spms/', include('your_appname.urls')),
b.项目setting.py中配置
python
# 添加所有终端都可访问网址
ALLOWED_HOSTS = ["*"]
......
# 添加模板根目录
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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',
],
},
},
]
......
# 注册App
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'your_appname',
]
......
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
......
# 注释中间件
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
7.运行启动
powershell
python manage.py runserver 0.0.0.0:8000
相关文档
链接: link