一、創(chuàng)建一個(gè)Project
django-admin startproject longgb246
產(chǎn)生目錄
longgb246/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
二堡纬、開啟服務(wù)
python manage.py runserver
端口號(hào):http://127.0.0.1:8000/
還可以改變端口聂受,或者IP
python manage.py runserver 8080
python manage.py runserver 0.0.0.0:8000
三、開啟app
python manage.py startapp polls
得到以下目錄
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
3.1 寫第一個(gè)view
打開polls/views.py
寫入
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
3.2 創(chuàng)建app的urls
得到以下目錄
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
urls.py
views.py
在urls.py
中寫到
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
3.3 創(chuàng)建項(xiàng)目的urls
在目錄的urls.py
下
寫入
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
把polls寫入其中烤镐。