亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

【Django】安裝Django并且開始接受外部訪問

標簽:
前端工具

正文之前

今天,本科的辅导员找到我,问我会不会做网站。。我比较虚,但是想想自己好歹做过。。所以就半推半就的接了话茬。。。  哎。。现在在愁找我建网站的老师会不会坑我。。

webp

正文

虽说只要求我做个静态网站,可是甲方?呵呵。。不信。。我还是复习下Django吧,不然到时候被坑了简直想死。。这就开始从头到尾写一下

下面是搭建环境的教程:

How to install Django

直接pip安装就好了。。不要看那么多的。。。当然,python是必须要有的。我就不说了。

然后就可以查看版本:

root@zhangzhaobo:/home/ubuntu# python -m django --version2.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:8000Performing 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:47Django 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 migrateOperations 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:10086Performing system checks...

System check identified no issues (0 silenced).
September 21, 2018 - 07:31:19Django 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 59428Invalid 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# lsdb.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# lsdb.sqlite3  manage.py  testsite
root@zhangzhaobo:/home/ubuntu/python/testsite# python manage.py runserver 0:10086Performing system checks...

System check identified no issues (0 silenced).
September 21, 2018 - 07:33:19Django 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 81348Not 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添加了*

然后就这样:

webp

正文之后

OK!收工,下课!!



作者:HustWolf
链接:https://www.jianshu.com/p/b43efa222e9e

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消