摘要:[05] Django View&URLs
View
修改 appName/views.py
from django.http import HttpResponse def home(request): s = "Hello World!" return HttpResponse(s) #參數用法 對應 url.py url(r'^Test/(\d+)/(\d+)/$', 'app.views.Test2', name='Test2'), def Test2(request,param1,param2): assert isinstance(request,HttpRequest) resultString=int(param1)+int(param2) return HttpResponse(resultString)
URLs
修改 ServerName/urls.py
from article.views import home urlpatterns = patterns('', ... url(r'^$', home), url(r'^WebAPI$', 'app.views.WebAPI', name='WebAPI'), url(r'^Test/(\d+)/(\d+)/$', 'app.views.Test2', name='Test2'), # )
補充:
url(r'^WebAPI$', 'app.views.WebAPI', name='WebAPI'), //可使用 reverse() 來獲取對應的網址 例:http: //localhost:56136/WebAPI
<a href="{% url 'WebAPI' %}">link</a>
QueryString view.py 呼叫方式 *當查詢無QueryString 會產生MultiValueDictKeyError at 錯誤
# /Test/?KeyName=123&KeyName2=Hello a1=request.GET['KeyName'] a2=request.GET['KeyName2']
url(r'^bookstore/', include('book.sale.urls')) #當專案越大時,url的內容會越雜,可額外新增urls.pay 做引用
python manage.py runserver