Speed up development with full-stack environments for every branch.

Learn More

session example [Python and Django]

864 Runs 6288 Views 1368 Copies
Saved

Saved

bhawani0915 49

bhawani0915
published 3 years ago

from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader
from django.template import RequestContext

def show_color(request):
    context = {}
    if "favorite_color" in request.session:
        context['favorite_color'] = request.session["favorite_color"]
        context['message'] = "Your current color is %s " % request.session["favorite_color"]
    else:
        context['favorite_color'] = None
        context['message'] = "You don't have a favorite color."
        
    template = loader.get_template('show_color.html')
    data = RequestContext(request, context)
    return HttpResponse(template.render(data))
        
        
def set_color(request):
    context = {}
    if "favorite_color" in request.GET:

        #return response
        request.session['favorite_color'] = request.GET["favorite_color"]
        return HttpResponseRedirect('/')

    else:
        context['message'] = ""
        
        template = loader.get_template('set_color.html')
        data = RequestContext(request, context)
        return HttpResponse(template.render(data))
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).