Python使用Django创建第一个项目
2024-10-21 05:53:14
一 必要环境安装
- 1首先确保安装了Python3,在此使用的系统为Ubuntu
@ubuntu:~$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
- 2安装pip
pip 是 Python 包管理工具,该工具提供了对Python 包的查找、下载、安装、卸载的功能。
使用sudo apt install python3-pip
命令安装pip
安装完使用此命令验证pip3是否已正确安装
fcj@ubuntu:~$ pip3 --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
- 3 使用pip安装一个Django包
看有些网友说是因为网络的问题,要使用国内的镜像源来加速
如果不加速,多试几次,也能安装:
或者使用镜像加速:比如豆瓣源
~$ pip3 install Django -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
二 创建Django项目
- 1安装完成后,可以进入到Python交互模式中,查看一下所安装的Django版本
fcj@ubuntu:~$ python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
2.2.1
>>>
- 2使用django-admin startproject xxx创建项目:
ubuntu:~/Desktop/code/PythonDemo$ django-admin startproject MyProject
Command 'django-admin' not found, but can be installed with:
sudo apt install python-django-common
根据提示使用:
sudo apt install python-django-common安装
如果还报错:
Cannot find installed version of python-django or python3-django
使用安装:
sudo apt-get install python3-django
然后即可正常创建项目!
fcj@ubuntu:~/Desktop/code/PythonDemo$ django-admin startproject MyProject
fcj@ubuntu:~/Desktop/code/PythonDemo$
- 3查看创建的项目文件
fcj@ubuntu:~/Desktop/code/PythonDemo$ tree
.
└── MyProject
├── manage.py
└── MyProject
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
2 directories, 5 files
- 4运行项目:python3 manage.py runserver
fcj@ubuntu:~/Desktop/code/PythonDemo/MyProject$ python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 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.
June 15, 2019 - 03:37:00
Django version 2.2.1, using settings 'MyProject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
项目运行成功
- 5解决项目运行时出现的报错:
You have 17 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
解决:
fcj@ubuntu:~/Desktop/code/PythonDemo/MyProject$ python3 manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... 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 sessions.0001_initial... OK
fcj@ubuntu:~/Desktop/code/PythonDemo/MyProject$ python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
June 15, 2019 - 03:41:46
Django version 2.2.1, using settings 'MyProject.settings'
Starting development server at http://127.0.0.1:8000/
最新文章
- UVA 10795 新汉诺塔问题
- 【WP开发】手电筒
- 边工作边刷题:70天一遍leetcode: day 81
- linphone3.4.0代码分析
- 屏幕序列Screen Sequences
- CSS3—六边形
- Linux命令(1):cd命令
- jQuery滚动条插件 – jquery.slimscroll.js
- C# 线程同步之排它锁/Monitor监视器类
- EF Unit Of Work
- javaWeb学习总结(1)- Tomcat服务器学习和使用(2)
- C语言--第0周作业
- Kafka分区与消费者的关系
- ASP.NET 管道
- 使用PHP+MySql+Ajax+jQuery实现省市区三级联动功能
- (转)mtr命令详解诊断网络路由
- Linux系统上传文件与下载文件命令
- JVM结构及堆的划分
- JavaScript中的构造函数 renturn
- 德哥PostgreSQL学习资料汇总(转)
热门文章
- kuma 学习一 minikube 安装
- 使用haproxy 2.0 prometheus metrics 监控系统状态
- nginx 访问控制之deny allow
- Java解决java.io.FileNotFoundException: E:\work\work (拒绝访问。)
- cgdsR 下载TCGA数据
- HGNC数据库 HUGO基因命名委员会
- 联想M7216NWA墨粉清零:
- MySQL悲观
- IDEA光标类的操作
- 泡泡一分钟:Real-Time Vehicle Detection from Short-Range Aerial Image with Compressed MobileNet