简体   繁体   English

使用Google App Engine在Django Python中进行会话处理

[英]Session handling in Django Python with Google App Engine

I am currently using Google App Engine to host my Django applications. 我目前正在使用Google App Engine托管Django应用程序。 I want to implement sessions in my Django application. 我想在我的Django应用程序中实现会话。 I cannot use the default session handling of Django because it is not supported in Google App Engine. 我无法使用Django的默认会话处理功能,因为Google App Engine不支持它。

After some research (basically Googling), I found that gae-sessions is the recommended library. 经过一些研究(基本上是Google搜索),我发现gae-sessions是推荐的库。 I tried to do wrap my head around the demo provided in the library but I could not understand how it works. 我试图绕过库中提供的演示,但是我不明白它是如何工作的。 Please help me in writing code which will set and maintain session variables for a user. 请帮助我编写代码,该代码将为用户设置和维护会话变量。 I am also open to using any other library with better documentation for session handling, so kindly point me towards one, if you know. 我也愿意使用其他任何具有更好文档的库来进行会话处理,如果您知道的话,请指向我。

gae-sessions is quite easy to use. gae-sessions非常易于使用。 You can set it up following the instructions provided on the github page. 您可以按照github页上提供的说明进行设置。 I will elaborate on the usage through an example of implementing login page. 我将通过一个实现登录页面的示例来详细说明用法。

Suppose that in your views.py you define a function as follows: 假设您在views.py定义了一个函数,如下所示:

class loginForm(forms.Form):
    email = forms.CharField(label='Email')
    password = forms.CharField(widget=forms.PasswordInput(),label = "Password")

def login(request):
    if request.method == 'POST':
        form = loginForm(request.POST)
        if form.is_valid():
            entered_email = form.cleaned_data['email']
            entered_pass = form.cleaned_data['password']
            try:
                _user = user.objects.get(email=entered_email)
                stored_password = _user.password
                if stored_password == entered_pass:
                    session = get_current_session()
                    if session.is_active():
                        session.terminate()
                    session['email'] = entered_email
                    return  'REDIRECT AS REQUIRED'
                else:
                    print "INCORRECT PASSWORD"
                    form = loginForm()
                    return render(request, 'login.html',{ 'form': form })
            except:
                print "EMAIL NOT REGISTERED"
                form = loginForm()
                return render(request, 'login.html',{ 'form': form })
    else:
        form = loginForm()
        return render(request, 'login.html',{ 'form': form })

In the above code, loginForm is used to define the input fields where the user will enter the credentials which he/she can use to login into the system. 在上面的代码中, loginForm用于定义输入字段,用户将在其中输入他/她可用于登录系统的凭据。 The login method is used to check the entered credentials against the database and if found to be valid a session is set using gae-sessions . login方法用于对照数据库检查输入的凭据,如果发现有效,则使用gae-sessions设置gae-sessions You will need to place the following import in your views.py 您需要将以下导入内容放入您的views.py

from gaesessions import get_current_session

In the login method presented above, we access the session using get_current_session() method which is imported from gae-sessions. 在上面显示的login方法中,我们使用从gae-sessions导入的get_current_session()方法访问会话。 Values for the session can be set according to your requirements. 会话的值可以根据您的要求进行设置。 I have set session['email'] value for maintaining the session. 我已经设置了session['email']值来维护会话。 The session can be terminated using simple session.terminate() . 可以使用简单的session.terminate()终止会话。

Be sure to follow the installation instructions provided here . 确保遵循此处提供的安装说明。 I hope you find this useful! 希望这个对你有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM