我是一个比较健忘的人,学得快忘得也快,而且做得笔记都混在一起了,查看起来很麻烦所以就写在博客里面,便于自己的查看,
本人萌新一个,有什么错误或者不足的地方希望各位大佬帮忙指正学习!
本人 的系统是centos7.5的虚拟机,和win10的系统
Python的项目文件是linux和windows共享的,关于centos虚拟机和windows共享文件的问题,不知道的可以看我的上一篇博客
由于我的centos系统是python2和Python3两个版本的,所以我在运行的时候用的是python3
#注:千万别用Python3.7.0的,这上面修改了好多的功能,在Python3.5和3.6都能运行的Django到了Python3.7就无法运行了了,切记!!
#还有就是我目前用的Django版本是1.8.2的,各个版本的Django的用法也是些许差异的
一.安装环境
1.linux环境为:
[root@centos7 ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) [root@centos7 ~]# uname -a Linux centos7.5-hxy 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux [root@centos7 ~]# python3 -V Python 3.6.3
2.windows环境为:
C:\Users\huxia\Desktop\py_script\django学习\learn_for_myself\day1\startDjango>python -V Python 3.5.2
二.安装
1.因为我的是已经安装了,所以会报已安装
C:\Users\huxia\Desktop\py_script\django学习\learn_for_myself\day1\startDjango>pip3 install django==1.8.2 Requirement already satisfied: django==1.8.2 in c:\python\python3\lib\site-packages (1.8.2)
2.如果是未安装的话是这样的:
[root@centos7 Python-3.6.3]# pip3 install django==1.8.2 Collecting django==1.8.2 Using cached https://files.pythonhosted.org/packages/4e/9d/2a1835ccbf8e1f0d6755d0e938ffd855f23886d055a7a18cc00a5224a99b/Django-1.8.2-py2.py3-none-any.whl Installing collected packages: django Successfully installed django-1.8.2 [root@centos7 Python-3.6.3]# pip3 install scrapy Collecting scrapy Using cached https://files.pythonhosted.org/packages/5d/12/a6197eaf97385e96fd8ec56627749a6229a9b3178ad73866a0b1fb377379/Scrapy-1.5.1-py2.py3-none-any.whl Collecting queuelib (from scrapy) Using cached https://files.pythonhosted.org/packages/4c/85/ae64e9145f39dd6d14f8af3fa809a270ef3729f3b90b3c0cf5aa242ab0d4/queuelib-1.5.0-py2.py3-none-any.whl Collecting PyDispatcher>=2.0.5 (from scrapy) Using cached https://files.pythonhosted.org/packages/cd/37/39aca520918ce1935bea9c356bcbb7ed7e52ad4e31bff9b943dfc8e7115b/PyDispatcher-2.0.5.tar.
我这里没有报错,如果各位遇到报错在百度下找解决办法吧,我没遇到就不往上写了!
3.下面来检验下我们是否安装成功!
[root@centos7 ~]# ipython3
Python 3.6.3 (default, Sep 29 2018, 10:58:43)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.0.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import django
In [2]: print(django.VERSION)
(1, 8, 2, 'final', 0)
In [3]: django.get_version()
Out[3]: '1.8.2'
In [4]: django.VERSION
Out[4]: (1, 8, 2, 'final', 0)
三.来个小实验(创建个简单的Django项目):
创建django项目:
[root@centos7 testDjango]# django-admin startproject myDjango
[root@centos7 testDjango]# cd myDjango/
[root@centos7 myDjango]# ls
manage.py myDjango
[root@centos7 myDjango]# tree
.
├── manage.py
└── myDjango
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
1 directory, 5 files
manage.py : 一个命令行工具,可以使用多种方式对django项目进行交互
__init__.py : 一个空文件,它告诉Python这个项目应该被看作Python包
settings.py: 项目配置文件
urls.py: 项目的url声明
wsgi.py:项目与wsgi兼容的web服务器入口
2.修改django的配置文件,让别的客户端也能访问这个系统的django(如果不配置的话就只能本机访问),
把这个改为:
别忘了逗号
ALLOWED_HOSTS = ['*',]
启动服务:
[root@centos7 myDjango]# python3 manage.py runserver 0.0.0.0:6666 Performing system checks... System check identified no issues (0 silenced). You have unapplied migrations; your app may not work properly until they are applied. Run 'python manage.py migrate' to apply them. September 29, 2018 - 05:10:19 Django version 1.8.2, using settings 'myDjango.settings' Starting development server at http://0.0.0.0:6666/ Quit the server with CONTROL-C. [29/Sep/2018 05:10:45]"GET / HTTP/1.1" 200 1767 [29/Sep/2018 05:10:45]"GET /favicon.ico HTTP/1.1" 404 1943 [29/Sep/2018 05:11:20]"GET / HTTP/1.1" 200 1767 [29/Sep/2018 05:11:21]"GET /favicon.ico HTTP/1.1" 404 1943
这里的应用报错不用管
然后启动服务就可以看到django运行起来了:
到浏览器上输入IP+端口
为了不让它有那个红色的提示我们继续往下创建应用:
[root@centos7 myDjango]# python3 manage.py startapp firstapp
执行迁移:执行sql语句生成数据表
[root@centos7 myDjango]# python3 manage.py migrate
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: admin, auth, sessions, contenttypes
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... 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 sessions.0001_initial... OK
然后再次启动Django
python3 manage.py runserver IP:端口
[root@centos7 myDjango]# python3 manage.py runserver 0.0.0.0:6666
Performing system checks...
System check identified no issues (0 silenced).
September 29, 2018 - 07:00:29
Django version 1.8.2, using settings 'myDjango.settings'
Starting development server at http://0.0.0.0:6666/
Quit the server with CONTROL-C.
这样就没有报错了,然后到浏览器上输入IP+端口
到此Django安装结束
转载于:https://blog.51cto.com/853056088/2287488