我的 Django 视图说它只需要一个参数并且给出了两个但只有一个
我正在开发一个 django 项目,当我尝试运行这个函数时,它返回只能给定一个参数,但只给定一个参数。
from django.http import HttpResponse, Http404
from django.template import Context
from django.template.loader import get_template
from django.contrib.auth.models import User
def main_page(request):
template = get_template('main_page.html')
variables = Context({
'head_title':u'Bookmarks!',
'page_title':u'Welcome to bookmarks!',
'page_body': u'store and share the bookmarks',
})
output = template.render(variables)
return HttpResponse(output)
URL 模式
from django.conf.urls.defaults import patterns, include, url
from socialnetwork.bookmarks.views import *
urlpatterns = patterns('',
(r'^user/(\w+)/$', main_page),
)
回溯:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/user/Colin/
Django Version: 1.3
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'socialnetwork.bookmarks']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
Exception Type: TypeError at /user/Colin/
Exception Value: main_page() takes exactly 1 argument (2 given)
I am working on a django project and when I try to run this function it returns that there can only be one argument given, but there is only one argument given.
from django.http import HttpResponse, Http404
from django.template import Context
from django.template.loader import get_template
from django.contrib.auth.models import User
def main_page(request):
template = get_template('main_page.html')
variables = Context({
'head_title':u'Bookmarks!',
'page_title':u'Welcome to bookmarks!',
'page_body': u'store and share the bookmarks',
})
output = template.render(variables)
return HttpResponse(output)
Url pattern
from django.conf.urls.defaults import patterns, include, url
from socialnetwork.bookmarks.views import *
urlpatterns = patterns('',
(r'^user/(\w+)/
Traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/user/Colin/
Django Version: 1.3
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'socialnetwork.bookmarks']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
Exception Type: TypeError at /user/Colin/
Exception Value: main_page() takes exactly 1 argument (2 given)
, main_page),
)
Traceback:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑您的 URL 模式捕获了其他一些变量。如果将 URL 模式附加到视图,并且该 URL 模式捕获任何值,则需要将该视图设置为在请求参数之上为每个捕获的值获取一个参数,该参数始终为第一的。因此,假设您有一个模式
myview()
需要像这样定义:I suspect that your URL pattern captures some other variable. If you attach a URL pattern to a view and that URL pattern captures any values, you need to have that view set up to take an argument for each captured value on top of the request argument, which is always first. So, let's say you've got a pattern
myview()
needs to be defined like so:request 始终是视图的第一个参数,位于您可能传递的其他参数之上。如果您调用视图并传递 1 个参数:
request is always the first argument of a view, on top of other arguments you may have passed. If you call the view passing it 1 argument:
您可能通过 url 传递附加参数。
You probably pass additional argument via url.