要求
请实现当url是http://127.0.0.1:8000/sum/n/(n是数字)时,返回的页面显示1-n的累加和的结果。
源码
views.py文件
#coding:utf-8urls.py文件
from django.shortcuts import render
from django.http import HttpResponse,Http404
# Create your views here.
def num_plus(request,plus):
try:
plus=int(plus)
except ValueError:
raise Http404()
sum=0
for x in range(plus+1):
sum+=x
html="<html><body><p>最终结果为%d</p></body></html>" % sum
return HttpResponse(html)
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
#url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r"^sum/(\d+)/$","timer.views.num_plus")
)
相关知识点
欢迎收听我的微信公众号