看完前一篇后,現(xiàn)在我們用Django進(jìn)行創(chuàng)建我們的第一個(gè)項(xiàng)目
安裝 Django 之后,您現(xiàn)在應(yīng)該已經(jīng)有了可用的管理工具 django-admin.py探熔。我們可以使用 django-admin.py 來(lái)創(chuàng)建一個(gè)項(xiàng)目:
root@iZ284shxZ:~# django-admin.py
Type 'django-admin.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runserver
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
可以看到有以上命令可以使用。
1.現(xiàn)在我們用startproject進(jìn)行創(chuàng)建第一個(gè)項(xiàng)目
root@iZ284shxZ:~# django-admin.py startproject HelloWorld
創(chuàng)建完后,可以看到在當(dāng)前主目錄下生成了HelloWorld
這個(gè)文件夾如叼。
進(jìn)行這個(gè)文件夾后,可以看到:
root@iZ28nshxZ:~# cd HelloWorld/
root@iZ28nshxZ:~/HelloWorld# ls
db.sqlite3 HelloWorld manage.py
root@iZ28nshxZ:~/HelloWorld# cd HelloWorld/
root@iZ28shxZ:~/HelloWorld/HelloWorld# ls
__init__.py __pycache__ settings.py urls.py wsgi.py
說(shuō)明:
HelloWorld
: 項(xiàng)目的容器穷劈。
HelloWorld
: 項(xiàng)目的容器笼恰。
HelloWorld/__init__.py
: 一個(gè)空文件,告訴 Python 該目錄是一
個(gè) Python 包歇终。
HelloWorld/settings.py
: 該 Django 項(xiàng)目的設(shè)置/配置社证。
HelloWorld/urls.py
: 該 Django 項(xiàng)目的 URL 聲明; 一份由 Django 驅(qū)動(dòng)的網(wǎng)站"目錄"。
HelloWorld/wsgi.py
: 一個(gè) WSGI 兼容的 Web 服務(wù)器的入口评凝,以便運(yùn)行你的項(xiàng)目追葡。
2 啟動(dòng)服務(wù)器:
root@iZ28shxZ:~/HelloWorld# python manage.py runserver 120.27.126.77:8000
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.
March 12, 2017 - 15:20:34
Django version 1.10.5, using settings 'HelloWorld.settings'
Starting development server at http://120.27.126.96:8000/
Quit the server with CONTROL-C.
3 訪問(wèn)
在瀏覽器中直接輸入120.27.126.77:8000
即可訪問(wèn)成功。
It worked!
Congratulations on your first Django-powered page.
Of course, you haven't actually done any work yet. 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!
4.PS
如果在訪問(wèn)時(shí)奕短,出現(xiàn)下面這種錯(cuò)誤宜肉,是因?yàn)樾枰バ薷捻?xiàng)目下的settings.py文件中的一個(gè)配置:
DisallowedHost at /
Invalid HTTP_HOST header: '120.27.96.77:8000'. You may need to add u'120.27.96.77' to ALLOWED_HOSTS.
在[]
里添加為全部可以允許的Hosts,修改文件為下面所示:
#ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['*']