我想制作一个函数来判断来自 url 的数字是奇数还是偶数。以下是关于法官的代码:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from django.http import HttpResponse,Http404
import datetime
def judge(reqest, number):
try:
number = int(number)
except ValueError:
raise Http404()
if number % 2 == 0:
html = "<html><body>%d is even!</body></html>" % (number)
else:
html = "<html><body>%d is odd!</body></html>" % (number)
return HttpResponse(html)
现在,我被困在了url.py
,我不知道这样的编辑是否会起作用:
from django.conf.urls.defaults import *
from views import *
urlpatterns = patterns('',
#(r'^$', time_now),
(r'^number/(.+)$', judge), # we just want the number in the url, not more or less
)
但我也想处理空输入,但是"Nothing input"
每次我尝试这个时页面都会给出:
def judge(reqest, number):
if number is None:
html = "nothing input!"
return HttpResponse(html)
try:
number = int(number)
except ValueError:
raise Http404()
if number % 2 == 0:
html = "<html><body>%d is even!</body></html>" % (number)
else:
html = "<html><body>%d is odd!</body></html>" % (number)
return HttpResponse(html)
我怎样才能解决这个问题?