【Django】安装Django并且开始接受外部访问
2018-09-21 本文已影响67人
张照博
正文之前
今天,本科的辅导员找到我,问我会不会做网站。。我比较虚,但是想想自己好歹做过。。所以就半推半就的接了话茬。。。 哎。。现在在愁找我建网站的老师会不会坑我。。
正文
虽说只要求我做个静态网站,可是甲方?呵呵。。不信。。我还是复习下Django吧,不然到时候被坑了简直想死。。这就开始从头到尾写一下
下面是搭建环境的教程:
直接pip安装就好了。。不要看那么多的。。。当然,python是必须要有的。我就不说了。
然后就可以查看版本:
root@zhangzhaobo:/home/ubuntu# python -m django --version
2.1
然后找个文件夹,开个项目吧:
root@zhangzhaobo:/home/ubuntu/python# django-admin startproject testsite
下面就是结构:
root@zhangzhaobo:/home/ubuntu/python# tree
.
├── apriori.py
└── testsite
├── manage.py
└── testsite
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
忽略最上头那个文件,没啥用的。。
第一次运行可能会出现这个:
root@zhangzhaobo:/home/ubuntu/python/testsite# python manage.py runserver 0:8000
Performing system checks...
System check identified no issues (0 silenced).
You have 15 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 21, 2018 - 07:30:47
Django version 2.1, using settings 'testsite.settings'
Starting development server at http://0:8000/
Quit the server with CONTROL-C.
按照要求来做就好了
^Croot@zhangzhaobo:/home/ubuntu/python/testsite# python 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 sessions.0001_initial... OK
然后可以运行啦!
root@zhangzhaobo:/home/ubuntu/python/testsite# python manage.py runserver 0:10086
Performing system checks...
System check identified no issues (0 silenced).
September 21, 2018 - 07:31:19
Django version 2.1, using settings 'testsite.settings'
Starting development server at http://0:10086/
Quit the server with CONTROL-C.
Invalid HTTP_HOST header: '104.216.175.73:10086'. You may need to add '104.216.175.73' to ALLOWED_HOSTS.
Bad Request: /
[21/Sep/2018 07:31:32] "GET / HTTP/1.1" 400 59428
Invalid HTTP_HOST header: '104.216.175.73:10086'. You may need to add '104.216.175.73' to ALLOWED_HOSTS.
Bad Request: /favicon.ico
[21/Sep/2018 07:31:34] "GET /favicon.ico HTTP/1.1" 400 59512
^Croot@zhangzhaobo:/home/ubuntu/python/testsite# ls
db.sqlite3 manage.py testsite
root@zhangzhaobo:/home/ubuntu/python/testsite# cd testsite/
root@zhangzhaobo:/home/ubuntu/python/testsite/testsite# l
__init__.py __pycache__/ settings.py urls.py wsgi.py
root@zhangzhaobo:/home/ubuntu/python/testsite/testsite# ls
__init__.py __pycache__ settings.py urls.py wsgi.py
root@zhangzhaobo:/home/ubuntu/python/testsite/testsite# vim settings.py
root@zhangzhaobo:/home/ubuntu/python/testsite/testsite# cd ..
root@zhangzhaobo:/home/ubuntu/python/testsite# ls
db.sqlite3 manage.py testsite
root@zhangzhaobo:/home/ubuntu/python/testsite# python manage.py runserver 0:10086
Performing system checks...
System check identified no issues (0 silenced).
September 21, 2018 - 07:33:19
Django version 2.1, using settings 'testsite.settings'
Starting development server at http://0:10086/
Quit the server with CONTROL-C.
[21/Sep/2018 07:33:23] "GET / HTTP/1.1" 200 16348
[21/Sep/2018 07:33:23] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[21/Sep/2018 07:33:24] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 82564
[21/Sep/2018 07:33:24] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 80304
[21/Sep/2018 07:33:25] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 81348
Not Found: /favicon.ico
[21/Sep/2018 07:33:25] "GET /favicon.ico HTTP/1.1" 404 1980
然后如果在一开始出现下列错误,只需小小调节就行:
DisallowedHost at /
Invalid HTTP_HOST header: '104.216.175.73:10086'. You may need to add u'104.216.175.73' to ALLOWED_HOSTS.
这个时候去setting.py里面改一下就好了
于是就去django-admin.py startproject testsite创建的项目中去修改 setting.py 文件:
ALLOWED_HOSTS = ['*'] #在这里请求的host添加了*
然后就这样:
正文之后
OK!收工,下课!!