Django 1.7:some_name()只需要2个参数(1个给定) [英] Django 1.7: some_name() takes exactly 2 arguments (1 given)

查看:177
本文介绍了Django 1.7:some_name()只需要2个参数(1个给定)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的view.py

this is my view.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)

这是我的urls.py来自django.conf.urls的

this is my urls.py

from django.conf.urls import patterns, url, include
from mysite.view import current_datetime, hours_ahead
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),

# url(r'^admin/', include(admin.site.urls)),
(r'^time/$', current_datetime),
#(r'^time/plus/\d{1,2}/$', hours_ahead),
url(r'$', 'mysite.view.hours_ahead', name='hours_ahead'),
)

当我尝试去这个本地主机:8000 / time / plus / 24 /
我有错误 hours_ahead()只需要2个参数(1给定)

when i try to go to this localhost:8000/time/plus/24/ I have the error hours_ahead() takes exactly 2 arguments (1 given)

推荐答案

您需要 captur e 与网址的偏移量:

You need to capture the offset from the url:

url(r'^time/plus/(\d+)/$', 'mysite.view.hours_ahead', name='hours_ahead'),

其中(\d +)是捕获一个或多个数字的捕获组。在 localhost:8000 / time / plus / 24 / 的情况下,它将捕获 24

where (\d+) is a capturing group that would capture one or more digits. In case of localhost:8000/time/plus/24/ it would capture 24.

这篇关于Django 1.7:some_name()只需要2个参数(1个给定)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆