Django NameError:name'request'未定义 [英] Django NameError: name 'request' is not defined

查看:1326
本文介绍了Django NameError:name'request'未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已导入这些模块:

from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response

然后我尝试像这样调用一个类:

and then I try to call a class like so:

sh = MyClass(request.FILES['img'])
sh.read_image()

但它会引发错误NameError:name请求'未定义,但我不明白为什么,因为我已经导入了必要的模块。

but it throws error "NameError: name 'request' is not defined", but I don't get it why, because I have imported necessary modules.

推荐答案

你在哪里上课?如果您想访问请求,您通常必须在视图中,如下所示:

Where are you making your class? If you want access to a request, you usually have to be inside a view, like so:

def my_view(request):
    sh = MyClass(request.FILES['img'])
    sh.read_image()
    return render_to_response('template.html')

这篇关于Django NameError:name'request'未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆