django - 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 页面。
谢谢!!
解决方案
目前尚不清楚您要在这里做什么。如果function
是一个视图,它应该request
作为第一个参数。如果它不是一个类方法,那么它不应该采用self
:
def function(request):
if request.method == 'GET':
return HttpResponse("hello")
else:
return HttpResponse("not get")
推荐阅读
- powerbi - DAX Measure 计算聚合数据,但按案例 ID 分组
- reactjs - react js中的道具是从父母到孩子还是从孩子到父母
- monit - 将 Monit 5.25.3 升级到 5.26 ...配置/安装
- mysql - 从同一个表中搜索逗号分隔的字符串
- acumatica - Acumatica:将快速流程添加到 Acumatica 应用程序?
- javascript - 具有延迟加载图像的 Puppeteer
- javascript - 如何使用没有 id 的 Javascript 访问元素?
- c# - PDFTRON 转换成功后不发布互操作进程
- python - 将 Keras 与 Ensemble Voting 分类器一起使用
- python - 不是 python 3 和 pandas 的 NaN 条件语句