Visual Studio Code搭建Django环境

Visual Studio Code搭建Django环境

该文章是结合了前辈们的经验进行更细致的整理,更适合小白参考。

创建虚拟环境:

终端下输入如下命令,其中envir是虚拟环境的文件夹名:

#Mac/Linux
python3 -m venv envir

#Windows
py -3 -m venv envir

Mac系统生成的文件夹默认位置是在mac/users/用户名/envir,我的文件路径是:/Users/doug/envir。

查看已经安装的package和版本

首先进入虚拟环境文件夹

Mac$3 ~:cd envir/
Mac$4 ~/envir:

执行命令“pip3 freeze”查看已经安装的package及版本。

Mac$4 ~/envir:pip3 freeze
asgiref==3.2.10
astroid==2.3.2
attrs==19.3.0
Automat==20.2.0
autopep8==1.5.3
beautifulsoup4==4.8.2
bs4==0.0.1
certifi==2019.11.28
cffi==1.14.0
chardet==3.0.4
constantly==15.1.0
cryptography==2.8
cssselect==1.1.0
Django==3.1
dnspython==1.16.0
Elixir==0.7.1
eventlet==0.25.1
get==2019.4.13
gitdb==4.0.5
GitPython==3.1.2
greenlet==0.4.15
hyperlink==19.0.0
idna==2.9
incremental==17.5.0
isort==4.3.21
lazy-object-proxy==1.4.3
lxml==4.4.1
mccabe==0.6.1
monotonic==1.5
mysqlclient==1.4.4
ntlm-auth==1.4.0
numpy==1.17.0
pandas==1.0.3
parsel==1.5.2
Pillow==7.1.2
post==2019.4.13
prettytable==0.7.2
Protego==0.1.16
public==2019.4.13
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycodestyle==2.6.0
pycparser==2.20
pycurl==7.43.0.3
PyDispatcher==2.0.5
PyHamcrest==2.0.2
pylint==2.4.3
pylint-django==2.3.0
pylint-plugin-utils==0.6
PyMySQL==0.10.0
pyOpenSSL==19.1.0
pyquery==1.4.0
python-dateutil==2.8.1
pytz==2019.2
query-string==2019.4.13
queuelib==1.5.0
rarfile==3.1
request==2019.4.13
requests==2.23.0
requests-ntlm==1.1.0
Scrapy==2.0.1
selenium==3.141.0
service-identity==18.1.0
six==1.12.0
smmap==3.0.4
soupsieve==2.0
SQLAlchemy==1.3.7
sqlparse==0.3.0
toml==0.10.1
tornado==6.0.4
Twisted==20.3.0
typed-ast==1.4.0
unrar==0.4
urllib3==1.25.8
uWSGI==2.0.18
virtualenv==16.7.2
w3lib==1.21.0
websockets==8.1
wrapt==1.11.2
wxPython==4.1.0
XlsxWriter==1.2.8
zope.interface==5.0.1

安装Django

执行“pip3 install django”或者指定版本安装(例:
“pip3 install django==1.11.11”)

Mac$5 ~/envir:pip3 install django
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Requirement already satisfied: django in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (3.1)
Requirement already satisfied: pytz in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from django) (2019.2)
Requirement already satisfied: sqlparse>=0.2.2 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from django) (0.3.0)
Requirement already satisfied: asgiref~=3.2.10 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from django) (3.2.10)

安装依赖

执行“pip3 install pylint pylint_django”
相关pylint_django的作用和安装,请参照大神的链接:在vscode中使用pylint-django插件解决pylint的一些不必要的错误提示

[Mac$6 ~/envir:pip3 install pylint pylint_django
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Requirement already satisfied: pylint in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (2.4.3)
Requirement already satisfied: pylint_django in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (2.3.0)
Requirement already satisfied: isort<5,>=4.2.5 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pylint) (4.3.21)
Requirement already satisfied: astroid<2.4,>=2.3.0 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pylint) (2.3.2)
Requirement already satisfied: mccabe<0.7,>=0.6 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pylint) (0.6.1)
Requirement already satisfied: pylint-plugin-utils>=0.5 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pylint_django) (0.6)
Requirement already satisfied: lazy-object-proxy==1.4.* in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.4.3)
Requirement already satisfied: typed-ast<1.5,>=1.4.0; implementation_name == "cpython" and python_version < "3.8" in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.4.0)
Requirement already satisfied: wrapt==1.11.* in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.11.2)
Requirement already satisfied: six==1.12 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.12.0)

输出安装好的 Django 和运行着的版本号

Mac$8 ~/envir:python3 -c "import django; print(django.get_version())"
3.1

新建项目

当前新建的项目为“mysite”。
执行命令:“django-admin.py startproject mysite”

Mac$3 ~/envir:django-admin.py startproject mysite

envir文件夹内会新建一个mysite的文件夹(mysite下还有一个mysite):
在这里插入图片描述

创建APP

进入项目文件夹:

Mac$4 ~/envir:cd mysite
Mac$5 ~/envir/mysite:

创建一个名为test1的app:
执行“python3 manage.py startapp test1”

Mac$6 ~/envir/mysite:python3 /Users/doug/envir/mysite/manage.py  startapp test1

创建后的目录结构:
在这里插入图片描述

运行Django

执行命令:“python3 manage.py runserver 9000”(端口号可以自己指定,或者执行:python3 manage.py runserver,默认端口号为8000)

Mac$7 ~/envir/mysite:python3 /Users/doug/envir/mysite/manage.py runserver 9000
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
September 14, 2020 - 01:11:08
Django version 3.1, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:9000/
Quit the server with CONTROL-C.

期间会出现以下红色警告:
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
这个警告可以先不用管。如果想要消除警告:我们可以根据后面的提示执行“python3 manage.py migrate”,如下:


 Mac$2 ~:python3 /Users/doug/envir/mysite/manage.py  migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK

在浏览器中输入url:http://localhost:9000,启动django项目。
启动成功的效果图如下:
Django项目搭建成功截图


Django项目结构

相关项目结构详细解析,可以参考前辈的博客:
链接: Django框架的项目结构详细解析.

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Visual Studio Code (VS Code) is a popular code editor that can be used for Django development. Here are the steps to set up Django in Visual Studio Code: 1. Install Visual Studio Code: Go to the official website (https://code.visualstudio.com/) and download the appropriate version for your operating system. Install it following the provided instructions. 2. Install Python: Django is a Python web framework, so you need to have Python installed. You can download Python from the official website (https://www.python.org/downloads/) and install it on your machine. 3. Create a virtual environment: It's a good practice to create a virtual environment for each Django project. Open the terminal in VS Code and navigate to your project directory. Run the following command to create a virtual environment: ``` python -m venv venv ``` 4. Activate the virtual environment: To activate the virtual environment, run the appropriate command based on your operating system: - On Windows: `venv\Scripts\activate` - On macOS/Linux: `source venv/bin/activate` 5. Install Django: With the virtual environment activated, install Django using pip: ``` pip install django ``` 6. Create a Django project: Run the following command to create a new Django project: ``` django-admin startproject myproject ``` 7. Open the project in Visual Studio Code: Use the File > Open Folder option in VS Code and select your Django project folder. 8. Install Python extension: In VS Code, go to the Extensions view (Ctrl+Shift+X or View > Extensions) and search for "Python". Install the official Python extension by Microsoft. 9. Configure the Python interpreter: Press Ctrl+Shift+P, type "Python: Select Interpreter", and choose the Python interpreter from the virtual environment you created earlier (venv). 10. Install Django extension: In VS Code, go to the Extensions view again and search for "Django". Install the "Django" extension by Baptiste Darthenay. 11. Start coding: Now you are ready to start coding your Django project in Visual Studio Code. You can use the integrated terminal, code navigation features, and other productivity tools provided by VS Code and the installed extensions. Remember to activate your virtual environment before running Django commands or starting the development server. I hope this helps you get started with Django development in Visual Studio Code! If you have any further questions, feel free to ask.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值