在Python web 框架中,Django 算是比較重的一個。
不同的版本,寫法還很不一樣。高版本對低版本也不兼容。
2010年用django 1.3 寫的一個有圖片上傳下載的小博客,后來運行不了了。關鍵是我自己還調不通了:(
后來也斷斷續續寫過幾個Django的項目。在生產環境下,對原生的django改造比較多。最近在基于Django搭一個web backend 框架。想到一步步整理一下,包括django原生的通過django-admin創建project, 創建app都整理記錄一下..
下面是通過django-admin創建一個最原始的django web.
$ django-admin startproject project_001
$ cd project_001
$ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 13 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.
August 08, 2017 - 05:34:41
Django version 1.11.3, using settings 'project_001.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
按照提示,執行 migrate
$ 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 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 sessions.0001_initial... OK
再次啟動:
$ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
August 08, 2017 - 05:44:02
Django version 1.11.3, using settings 'project_001.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
訪問頁面:
可以通過瀏覽器,也可以通過linux 命令w3m在控制臺訪問
$ w3m http://127.0.0.1:8000
It worked!
Congratulations on your first Django-powered page.
Next, start your first app by running python manage.py startapp [app_label].
You're seeing this message because you have DEBUG = True in your Django settings file and you haven't configured any URLs. Get to work!
創建app
$ python manage.py startapp app_001
通過 http://ip:8000/ 在瀏覽器中進行訪問,提示:
無法訪問此網站
172.28.32.49 拒絕了我們的連接請求。
請在 Google 中搜索“172 8000”
ERR_CONNECTION_REFUSED
修改 project_001/settings.py 文件
ALLOWED_HOSTS = ['*']
#ALLOWED_HOSTS = []
再次啟動 , 啟動項中新增 0.0.0.0:8000
$ python manage.py runserver 0.0.0.0:8000
Performing system checks...
System check identified no issues (0 silenced).
August 08, 2017 - 05:51:40
Django version 1.11.3, using settings 'project_001.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
[08/Aug/2017 05:51:43] "GET / HTTP/1.1" 200 1716
此時,通過瀏覽器可以訪問。默認提供 /admin url.
http://172.28.32.49:8000/admin
image.png
image.png
創建一組賬號:root/jia123456
$ python manage.py createsuperuser
Username (leave blank to use 'root'):
Email address:
Password:
Password (again):
Superuser created successfully.
image.png
此時一個簡單的django web 就算建立了。
具體地址見github, master 分支
https://github.com/jiaxiaolei/my_django_project