Django-类型对象'HttpRequest'没有属性'method'

时间:2018-10-25 21:20:12

标签: django python-3.x

我遇到属性错误

  

类型对象'HttpRequest'没有属性'方法'

但是HttpRequest根据Django文档具有属性“ method”。

我的views.py:

from django.http import HttpRequest, HttpResponse
import myapp.pyfile

def function(self):
    request = HttpRequest
    if request.method == 'GET':
       return HttpResponse(pyfile_function())

我的urls.py:

from django.conf.urls import url
from myapp.views import function, index

urlpatterns = [
   url(r'^myapp/$', index),
   url(r'^myapp/function_url/$', function)
]

我的pyfile_function:

def pyfile_function():
    x = DAO.qryListAutpagsCreateFolder()
    field1 = x[0]
    field2 = str(x[1])
    field3 = x[2].strftime('%d/%m/%Y')

    i = 1

    while i <= len(x):
        return("""<tr class=''>
            <td class='' colspan='1'>
                <span class=''"""+field1+"""</span>
            </td>
            <td class='' colspan='1'>
                <span class=''>"""+field2+"""</span>
            </td>
            <td class='' colspan='1'>
                <span class=''>"""+field3+"""</span>
            </td>
            <td class='' colspan='1'>
                <label class='form-field'>
                    <div>
                        <label class='form-checkbox '>
                            <input name='checkbox' type='checkbox'>
                            <span></span>
                        </label>
                    </div>
                </label>
            </td>
        </tr>""")
        i = i + 1

跟踪:

File "/usr/lib/python3/dist-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/path/myapp/views.py" in function
  12.        return HttpResponse(function())

Exception Type: TypeError at /myapp/function_url/
Exception Value: function() missing 1 required positional argument: 'request'

我正在使用Django 1.8和Python 3.5。

pyfile_function应该呈现带有动态列表的HTML页面。

谢谢!

2 个答案:

答案 0 :(得分:1)

目前尚不清楚您要在这里做什么。如果function是视图,则应将request作为第一个参数。如果不是类方法,则不应使用self

def function(request):
    if request.method == 'GET':
        return HttpResponse("hello")
    else:
        return HttpResponse("not get")

答案 1 :(得分:-1)

缺少括号,请替换此行

request = HttpRequest

使用

request = HttpRequest()