Django错误:接受0个位置参数,但是给出了1。

时间:2021-12-10 23:21:00

I'm trying to set up django on an EC2 ubuntu instance.

我正在尝试在一个EC2 ubuntu实例上设置django。

In my view I have:

在我看来,我有:

from django.shortcuts import render, HttpResponse
import requests


def test(request):
    return HttpResponse('hi')

urls.py have:

url。py:

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'tp.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^index/$', 'tp1.views.index', name='index'),
    url(r'^test/$', 'tp1.views.test', name='test'),    

    # url(r'^admin/', include(admin.site.urls)),
)

I'm getting the error above. What am I doing wrong?

我得到了上面的误差。我做错了什么?

edit;

编辑;

 Environment:    


 Request Method: GET
 Request URL: http://52.10.**-**:8000/test/  

 Django Version: 1.7.5
 Python Version: 3.4.0
 Installed Applications:
 ('django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'tp1')
 Installed Middleware:
 ('django.contrib.sessions.middleware.SessionMiddleware',
  'django.middleware.common.CommonMiddleware',
  'django.middleware.csrf.CsrfViewMiddleware',
  'django.contrib.auth.middleware.AuthenticationMiddleware',
  'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
  'django.contrib.messages.middleware.MessageMiddleware',
  'django.middleware.clickjacking.XFrameOptionsMiddleware')  


 Traceback:
 File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
   111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)  

 Exception Type: TypeError at /test/
 Exception Value: test() takes 0 positional arguments but 1 was given

1 个解决方案

#1


0  

The code in your question looks fine, but the test() view function takes one positional argument (request), so the code in your question is not the code that raised that exception.

您的问题中的代码看起来不错,但是test()视图函数接受一个位置参数(请求),所以您的问题中的代码不是引发该异常的代码。

Did you leave out any code (maybe you re-defined test later in the views.py file)? Is that code at the top from tp1/views.py or some other file?

您是否遗漏了任何代码(可能您稍后在视图中重新定义了测试)。py文件)?是tp1/views上面的代码吗?py还是其他文件?

#1


0  

The code in your question looks fine, but the test() view function takes one positional argument (request), so the code in your question is not the code that raised that exception.

您的问题中的代码看起来不错,但是test()视图函数接受一个位置参数(请求),所以您的问题中的代码不是引发该异常的代码。

Did you leave out any code (maybe you re-defined test later in the views.py file)? Is that code at the top from tp1/views.py or some other file?

您是否遗漏了任何代码(可能您稍后在视图中重新定义了测试)。py文件)?是tp1/views上面的代码吗?py还是其他文件?