在Python web 框架中氓辣,Django 算是比較重的一個(gè)。
不同的版本埂息,寫法還很不一樣皮璧。高版本對(duì)低版本也不兼容舟扎。
2010年用django 1.3 寫的一個(gè)有圖片上傳下載的小博客,后來運(yùn)行不了了悴务。關(guān)鍵是我自己還調(diào)不通了:(
后來也斷斷續(xù)續(xù)寫過幾個(gè)Django的項(xiàng)目睹限。在生產(chǎn)環(huán)境下,對(duì)原生的django改造比較多讯檐。最近在基于Django搭一個(gè)web backend 框架羡疗。想到一步步整理一下,包括django原生的通過django-admin創(chuàng)建project, 創(chuàng)建app都整理記錄一下..
下面是通過django-admin創(chuàng)建一個(gè)最原始的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.
按照提示别洪,執(zhí)行 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
再次啟動(dòng):
$ 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在控制臺(tái)訪問
$ 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!
創(chuàng)建app
$ python manage.py startapp app_001
通過 http://ip:8000/ 在瀏覽器中進(jìn)行訪問,提示:
無法訪問此網(wǎng)站
172.28.32.49 拒絕了我們的連接請(qǐng)求蕉拢。
請(qǐng)?jiān)?Google 中搜索“172 8000”
ERR_CONNECTION_REFUSED
修改 project_001/settings.py 文件
ALLOWED_HOSTS = ['*']
#ALLOWED_HOSTS = []
再次啟動(dòng) , 啟動(dòng)項(xiàng)中新增 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
此時(shí)特碳,通過瀏覽器可以訪問诚亚。默認(rèn)提供 /admin url.
http://172.28.32.49:8000/admin
創(chuàng)建一組賬號(hào):root/jia123456
$ python manage.py createsuperuser
Username (leave blank to use 'root'):
Email address:
Password:
Password (again):
Superuser created successfully.
此時(shí)一個(gè)簡(jiǎn)單的django web 就算建立了晕换。
具體地址見github, master 分支
https://github.com/jiaxiaolei/my_django_project