Skip to main content
 首页 » 编程设计

python之Django / python "multiple repeat"

2024年11月01日38zhwl

我正在研究 python 基础知识。我遇到了这样的问题,我无法弄清楚。

View .py

# -*- coding: utf-8 -*- 
 
from django.http import HttpResponse 
import datetime 
 
def hello(request): 
    return HttpResponse("Hello, World!") 
 
def time(request): 
    now = datetime.datetime.now() 
    html = "<html><body>Teraz jest %s</body></html>" %now 
    return HttpResponse(html) 
 
def time_offset(request, offset): 
    delta = int(offset) 
    dt = datetime.datetime.now() + datetime.timedelta(hours=offset) 
    assert False 
    shtml = "<html><body>za %s godzin będzie %s</body></html>" % (delta,dt) 
    return HttpResponse(shtml) 

网址.py

# -*- coding: utf-8 -*- 
from django.conf.urls import patterns, include, url 
 
 
urlpatterns = patterns('', 
    url(r'^hello$', 'mysite.views.hello', name='hello'), 
    url(r'^time$', 'mysite.views.time', name='time'), 
    url(r'^time\plus\(/d+{1,2})$', 'mysite.views.time_offset', name='time_offset'),   ) 

都是关于我 url 的最后一行。我正在那里激活我的新 View “time_offset”,它一定有问题,但我不知道是什么?我无法让这个 View 工作。

谢谢你的帮助,干杯!

错误代码:

error at /time/plus/2 
multiple repeat 
Request Method: GET 
Request URL:    http://127.0.0.1:8000/time/plus/2 
Django Version: 1.4.5 
Exception Type: error 
Exception Value:     
multiple repeat 
Exception Location: /usr/lib/python2.7/re.py in _compile, line 242 
Python Executable:  /usr/bin/python 
Python Version: 2.7.3 
Python Path:     
['/home/maze/Dokumenty/djcode/mysite', 
 '/usr/lib/python2.7', 
 '/usr/lib/python2.7/plat-linux2', 
 '/usr/lib/python2.7/lib-tk', 
 '/usr/lib/python2.7/lib-old', 
 '/usr/lib/python2.7/lib-dynload', 
 '/usr/local/lib/python2.7/dist-packages', 
 '/usr/lib/python2.7/dist-packages', 
 '/usr/lib/python2.7/dist-packages/PIL', 
 '/usr/lib/python2.7/dist-packages/gtk-2.0', 
 '/usr/lib/pymodules/python2.7'] 
Server time:    Tue, 10 Dec 2013 04:40:17 -0600 

请您参考如下方法:

正则表达式结合了两个重复修饰符(+, {1, 2})

>>> re.compile(r'\d+{1,2}') 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
  File "C:\Python27\lib\re.py", line 190, in compile 
    return _compile(pattern, flags) 
  File "C:\Python27\lib\re.py", line 242, in _compile 
    raise error, v # invalid expression 
sre_constants.error: multiple repeat 

仅使用 + 或 {1,2}:

>>> re.compile(r'\d{1,2}') 
<_sre.SRE_Pattern object at 0x00000000025C5458> 
>>> re.compile(r'\d+') 
<_sre.SRE_Pattern object at 0x0000000002A454F0> 

顺便说一句,似乎代码交换了 \ 和 /。

... r'^time\plus\(/d+{1,2})$ ... 
#          ^    ^ ^