longgb246的博客

Tutorial_1 Request and Response

一、创建一个Project

1
django-admin startproject longgb246

产生目录

1
2
3
4
5
6
7
longgb246/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py

二、开启服务

1
python manage.py runserver

端口号:http://127.0.0.1:8000/
还可以改变端口,或者IP

1
2
python manage.py runserver 8080
python manage.py runserver 0.0.0.0:8000

三、开启app

1
python manage.py startapp polls

得到以下目录

1
2
3
4
5
6
7
8
9
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py

3.1 写第一个view

打开polls/views.py
写入

1
2
3
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")

3.2 创建app的urls

得到以下目录

1
2
3
4
5
6
7
8
9
10
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
urls.py
views.py

urls.py中写到

1
2
3
4
5
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]

3.3 创建项目的urls

在目录的urls.py
写入

1
2
3
4
5
6
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写入其中。

坚持原创技术分享,您的支持将鼓励我继续创作!