Django 类问题

发布于 2024-11-09 08:11:46 字数 1523 浏览 0 评论 0原文

首先请原谅我的英语,这不是最好的。

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请你别敷衍 2024-11-16 08:11:46

很明显,您不能在 einloggen 类的 __init__ 中使用 request 变量,因为,坦率地说,您不发送请求变量到构造函数中。

我也看不到您在视图中的任何位置创建 einloggen 对象,但您可能应该这样做:

class einloggen:
    def __init__(self, request):
        self.Username = request.POST.get('username')

然后在您的视图中(您获得了请求变量):

def anges(request):
    myobj = einloggen(request)

但是,Django已经有一个认证系统。使用它你会更好。您可以使用漂亮的装饰器来让“保护”视图变得非常简单和美好。

Quite obvious that you can't use the request variable in __init__ in the einloggen 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:

class einloggen:
    def __init__(self, request):
        self.Username = request.POST.get('username')

and then in your view (where you've got the request variable):

def anges(request):
    myobj = einloggen(request)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文