我的 Django 视图说它只需要一个参数并且给出了两个但只有一个

发布于 2024-11-30 00:46:02 字数 1792 浏览 0 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北方。的韩爷 2024-12-07 00:46:02

我怀疑您的 URL 模式捕获了其他一些变量。如果将 URL 模式附加到视图,并且该 URL 模式捕获任何值,则需要将该视图设置为在请求参数之上为每个捕获的值获取一个参数,该参数始终为第一的。因此,假设您有一个模式

(r'^mainpage(\d+)/

myview() 需要像这样定义:

def myview(request, digit):
    # do something
, myview)

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

(r'^mainpage(\d+)/

myview() needs to be defined like so:

def myview(request, digit):
    # do something
, myview)

myview() needs to be defined like so:

冰雪之触 2024-12-07 00:46:02

request 始终是视图的第一个参数,位于您可能传递的其他参数之上。如果您调用视图并传递 1 个参数:

def main_page(request, arg1):
  code ...

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:

def main_page(request, arg1):
  code ...
与风相奔跑 2024-12-07 00:46:02

您可能通过 url 传递附加参数。

You probably pass additional argument via url.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文