Django 类问题
首先请原谅我的英语,这不是最好的。
我对 django 和 python 很陌生,我尝试编写用户身份验证程序。 我使用了 django 文档,并且以下代码一切正常:
def anges(request):
username = []
password = []
if request.method == "POST":
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
render_to_response ('registration/login.html', {'username': username, 'password': password})
if user is not None:
if user.is_active:
login(request, user)
angestellte_list = Employee.objects.all().order_by('lastname')
return render_to_response(("emp/angestellte.html"), {'angestellte_list': angestellte_list})
else:
return HttpResponse('disabled account')
else:
return HttpResponse('invalid login')
else:
return render_to_response ('registration/login.html', {'username':username, 'password':password})
但这只是一个函数,我想在 view.py 中使用这个面向对象的其他函数,因为 DRY。 这只是第一个测试,但它不起作用,因为调试器说:“全局名称'请求'未定义” 这是我的代码:
class einloggen:
def __init__(self):
self.Username = request.POST['username']
def angestellte(self):
return HttpResponse("hello")
如何在类中使用 request 变量,或者还有什么需要考虑的吗?
first excuse me for my english it isn't the best one.
I'm pretty new to django and python and i try to programm a user authentification.
I used the django documentation and everything works fine with these code below:
def anges(request):
username = []
password = []
if request.method == "POST":
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
render_to_response ('registration/login.html', {'username': username, 'password': password})
if user is not None:
if user.is_active:
login(request, user)
angestellte_list = Employee.objects.all().order_by('lastname')
return render_to_response(("emp/angestellte.html"), {'angestellte_list': angestellte_list})
else:
return HttpResponse('disabled account')
else:
return HttpResponse('invalid login')
else:
return render_to_response ('registration/login.html', {'username':username, 'password':password})
But this is just a function and i want to use this object oriented for my other functions in my views.py, because of DRY.
This is just a first test but it doesn't works because the debugger says:"global name 'request' is not defined"
That's my code:
class einloggen:
def __init__(self):
self.Username = request.POST['username']
def angestellte(self):
return HttpResponse("hello")
How can I use the request variable in classes or is there anything more to consider?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很明显,您不能在
einloggen
类的__init__
中使用request
变量,因为,坦率地说,您不发送请求变量到构造函数中。我也看不到您在视图中的任何位置创建
einloggen
对象,但您可能应该这样做:然后在您的视图中(您获得了请求变量):
但是,Django已经有一个认证系统。使用它你会更好。您可以使用漂亮的装饰器来让“保护”视图变得非常简单和美好。
Quite obvious that you can't use the
request
variable in__init__
in theeinloggen
class, because, quite frankly, you don't send the request variable in to the constructor.I can't see you making a
einloggen
object anywhere in your view either, but you should probably to something like:and then in your view (where you've got the request variable):
However, Django already has an authentication system. And you'd be much better off using that. You can use the beautiful decorators to make it really easy and nice to «protect» views.