1.django-admin startproject mysite 创建Django程序
2.python manage.py startapp yourapp 创建一个你的应用
3.在views.py中写应用处理函数
(1)导入需要的包
本次用到reverse函数和HttpResponse
from django.http import HttpResponse
from django.core.urlresolvers import reverse
def books_hello(request):
s = reverse('new_books_hello')
return HttpResponse('<a href =\"'+s+'\ ">'+'this is a new address'+'</a>')
def new_books_hello(request):
return HttpResponse('welcome new books hello')
4.设置路由
(1)导入应用包views.py
以应用为books为例
from books import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^books_hello', views.books_hello, name='books_hello'),
url(r'^new_books_hello', views.new_books_hello, name='new_books_hello'),
]
5.最后在setting.py中加入你的应用# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'books',#应用
]
最后运行程序
python manage.py runserver