Django中的SyntaxError

时间:2014-01-10 11:10:00

标签: python django

我正在尝试遵循django教程,(第4章,http://www.djangobook.com/en/2.0/chapter04.html)。但是下面的代码会引发语法错误。

错误:

Request Method: GET
Request URL:    http://127.0.0.1:1222/hello/
Exception Type: SyntaxError
Exception Value:    invalid syntax (views.py, line 23)
Exception Location: /home/milad/djangobook/djangobook/urls.py in <module>, line 2

urls.py

from django.conf.urls import patterns, include, url
from djangobook.views import hello, showtime, plustime

urlpatterns = patterns('',('^hello/$',hello),('^time/$',showtime),(r'^time/plus/(\d{1,2})/$',plustime),
)

views.py

from django.http import HttpResponse 
from django.template.loader import get_template
from django.template import Context
import datetime 

def hello(request):
    return HttpResponse ("Hello Dear Django!")

def showtime(request):
    now = datetime.datetime.now()
    t = get_template('showtime.html')
    html = t.render(Context({'time':now}))
    return HttpResponse(html)

def plustime(request,plus):
    try:
        plus = int(plus)
    except ValueError:
        raise Http404()
    now = datetime.datetime.now() + datetime.timedelta(hours=plus)
    t = get_template('plustime.html')   
    html = t.render(Context({'plus1':now})
    return HttpResponse(html)

1 个答案:

答案 0 :(得分:4)

您在前一行缺少右括号:

html = t.render(Context({'plus1':now})
#          --- 1     --2          -- 2 but no 1

在那里添加)

html = t.render(Context({'plus1':now}))