首页 > 解决方案 > Django - 类型对象'HttpRequest'没有属性'方法'

问题描述

我收到一个属性错误

类型对象“HttpRequest”没有属性“方法”

但 HttpRequest 根据 Django 文档具有“方法”属性。

我的意见.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 页面。

谢谢!!

标签: djangopython-3.x

解决方案


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

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

推荐阅读