gpt4 book ai didi

python - Python/Django 中的 URL 映射

转载 作者:太空宇宙 更新时间:2023-11-04 09:18:25 24 4
gpt4 key购买 nike

我刚开始学习 Django 和 Python。

我使用来自 djangobook.com 的在线图书

在第 3 章(http://djangobook.com/en/1.0/chapter03/)中,我正在尝试将当前时间增加 x 小时的示例。我的文件如下:

urls.py

from django.conf.urls.defaults import patterns, include, url
from mysite.views import current_datetime

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

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

views.py

from django.http import HttpResponse
import datetime

def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)

def hours_ahead(request, offset):
offset = int(offset)
dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt)
return HttpResponse(html)

但如果我尝试导航到:http://127.0.0.1:8000/time/plus/5/ ,我在/time/plus/5/得到一个 NameError。我错过了什么吗?

谢谢。

编辑

在这里转储 - http://pastebin.com/Hn3aFLzR

最佳答案

您忘记在 urls.py 中导入 hours_ahead:

from mysite.views import current_datetime, hours_ahead

关于python - Python/Django 中的 URL 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5674353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com