一、创建一个Project
django-admin startproject longgb246
产生目录
longgb246/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
二、开启服务
python manage.py runserver
端口号: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 写第一个view
打开polls/views.py
写入
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
3.2 创建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 创建项目的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写入其中。