0

現在の日付と時刻を特定の時間オフセットして表示するビューを作成しています。目標は、ページ /time/plus/1/ で日付と時刻を 1 時間単位で表示し、ページ /time/plus/2/ で日付と時刻を 2 時間で表示し、ページ /time/ plus のサイトを設計することです。 /3/ は、日付と時刻を 3 時間単位で表示します。これはviews.pyの私の関数です:

def hours_ahead(request, offset):
    try:
        offset = int(offset)
    except ValueError:
        raise Http404()
    dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
    html = "In %s hour(s), it will be %s." % (offset, dt)
    return HttpResponse(html)

これは私のurl.pyファイルの内容です:

from django.conf.urls import patterns, include, url
from aplicacion1.views import hours_ahead

urlpatterns = patterns('',
    url(r'^time/plus/(d{1,2})/$', hours_ahead),)

views.py のパターンは、1 桁または 2 桁の数字のみを受け入れる必要があります。それ以外の場合、Django は「ページが見つかりません」というエラーを表示する必要があります。URLhttp://127.0.0.1:8000/time/plus/(no hours)も 404 エラーをスローする必要があります。しかし、サーバーを実行して のような URL にアクセスしようとするとhttp://127.0.0.1:8000/time/plus/24/、次のエラーが表示されます。

Page not found (404)
Request Method: GET
Request URL:    

http://127.0.0.1:8000/time/plus/24/
Using the URLconf defined in cibernat.urls, Django tried these URL patterns, in this order:
^$
^time/$
^time/plus\d+/$
^admin/doc/
^admin/
The current URL, time/plus/24/, didn't match any of these.

私が犯しているエラーは何ですか?正規表現が正しいように見えます

4

1 に答える 1

3

あなたのURLパターンは

url(r'^time/plus/(\d{1,2})/$', hours_ahead),

\あなたは前を忘れたd

于 2013-06-25T16:12:33.657 に答える