Python使用Django创建第一个项目

Posted qqmb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python使用Django创建第一个项目相关的知识,希望对你有一定的参考价值。

一 必要环境安装

  • 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版本
[email protected]:~$ 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
然后即可正常创建项目!

[email protected]:~/Desktop/code/PythonDemo$ django-admin startproject MyProject
[email protected]:~/Desktop/code/PythonDemo$ 
  • 3查看创建的项目文件
[email protected]:~/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
[email protected]:~/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.

打开: http://127.0.0.1:8000/
技术图片

项目运行成功

  • 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解决:

[email protected]:~/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
[email protected]:~/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/

以上是关于Python使用Django创建第一个项目的主要内容,如果未能解决你的问题,请参考以下文章

django创建第一个项目helloworld

python安装Django并创建第一个项目

Python - 自学django,上线一套资产管理系统

创建第一个Django项目

PyCharm 创建Django 第一个项目

学习python的第五天