Django的虚拟环境和简单搭建
1. 创建虚拟环境
1. 首先新建一个文件夹
![]() 
2. 在PyCharm终端中切换到这个文件夹,输入 python -m venv 环境名 创建虚拟环境
![]() 
3.此时文件夹下出现了名为packages
![]() 
2. 激活虚拟环境
1.在终端中输入 环境名\Scripts\activate 激活环境,要停止虚拟环境可以输入deactivate
![]() 
2.如果激活报错
# 问题:
  Windows PowerShell中无法加载文件 xxxScriptsActivate.ps1,因为在此系统上禁止运行脚本
# 解决方案:
  https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.3
  输入-进入PowerShell模式:
    Set-ExecutionPolicy -Scope CurrentUs
  PowerShell模式下输入:
    Unrestricted  
3.安装Django
1. 激活条件下输入 pip install django
![]() 
4.在Django下创建项目
1. 输入 django-admin startproject 项目名 . (不要忘记这个点)
![]() 
2. 此时文件夹下已经创建好项目
![]() 
5.创建应用程序(APP)
 
1. 使用pycharm打开文件,激活虚拟环境,执行startapp命令
![]() 
2. 执行完成后会发现文件夹下多出了bbc_list文件夹
![]() 
6.项目关联APP
1. 在项目中的settings.py中关联APP中的apps.py中的类
# 项目关联APP
  INSTALLED_APPS = ['bbc_list.apps.BbcListConfig']
# 如果想要将页面改为中文,可以在settings.py文件中设置	
  LANGUAGE_CODE = 'zh-hans'
  TIME_ZONE = 'Asia/Shanghai'
  
![]() 
7.在APP里编写视图函数
- from django.shortcuts import render,HttpResponse
      def index(request):
        return HttpResponse("欢迎使用")
 ![]() 
8.在项目中的urls.py中关联APP的视图view.py
    - from bbc_list import views
      urlpatterns = [
      path('index/', views.index),] # index/配置的访问路由, views.index:views.py中的方法
 ![]() 
9.启动django项目
    - 命令行执行 : python manage.py runserver
    - 访问路径 : http://127.0.0.1:8000/index/
 ![]()