一、在网页上做加减法
1.采用/add/?a=5&b=6这样GET方法进行
修改Auto下面的views.py
def add(request):
a= request.GET['a']
b= request.GET['b']
c=int(a)+int(b)
return HttpResponse(str(c))
修改AutoTestPlatForm下urls.py:
urlpatterns = [
url(r'^index/$','Auto.views.hello'),
url(r'^add/','Auto.views.add',name='add')
]
重启服务器:
python manage.py runserver
2.采用/add/7/8/这样的网址的方式
修改Auto下面的views.py
def add2(request,a,b):
c=int(a) + int(b)
return HttpResponse(str(c))
修改AutoTestPlatForm下urls.py:
urlpatterns = [
url(r'^index/$','Auto.views.hello'),
url(r'^add/','Auto.views.add',name='add'),
url(r'^add2/(\d+)/(\d+)/$','Auto.views.add2',name='add2'),
]
二、url中的name
给URL 起个名字
url(r'^add/','Auto.views.add',name='add'),
url(r'^add2/(\d+)/(\d+)/$','Auto.views.add2',name='add2'),
起到的一个作用可以说是跳转
比如:目前存在一个网址是/add2/4/5,需求将网址改为/new_add/4/5,那么该如何处理?改掉所有存在add2的文件?
这里我们应该做一个跳转来处理:
修改views:
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
def old_redirect(request, a, b):
return HttpResponseRedirect(reverse('add2', args=(a, b))
)
修改urls.py
url(r'^add2/(\d+)/(\d+)/$',Auto.views.old_redirect),
url(r'^new_add/(\d+)/(\d+)/$',Auto.views.add2,name='add2'),