一 必要環境安裝
- 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.
打開: 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
解決:
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/