django reverse方法


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM