寫一個能夠查看投票和具有投票功能,能在後臺增刪改查投票的app,使用django2.0,只支持python3.4+版本。php
建立app,將會自動生產主體框架的代碼python
$ django-admin startproject mysite
若是你的後臺安裝過php,最好不要吧代碼放在根目錄下,不利於安全。django
將會生成一下目錄安全
mysite/ manage.py mysite/ __init__.py settings.py urls.py wsgi.py
運行app
python manage.py runserver
後面能夠直接添加端口或則IP:端口框架
python manage.py runserver 8080
python manage.py runserver 0:8000url
運行後訪問網頁會提示You may need to add 'xxxx to ALLOWED_HOSTS,在setting裏面添加ALLOWED_HOSTS = ['xxx],xxx 表明hostIP,就行了,這裏也能夠誒用正則‘*’ 去匹配code
建立app,會自動生成部分文件,一個項目能夠包含多個app,一個app能夠被用於多個項目server
$ python manage.py startapp polls
1,寫app view代碼get
from django.http import HttpResponse def index(request): return HttpResponse("Hello, world. You're at the polls index."),]
2,建立url.py文件
polls/urls.py
from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ]
3,添加url到項目 url.py文件
mysite/urls.py
from django.urls import include, path from django.contrib import admin urlpatterns = [ path('polls/', include('polls.urls')), path('admin/', admin.site.urls), ]
include()
很方便修改某個app對應的url, 推薦使用,運行
python manage.py runserver