<?php
session_start();
$_SESSION['username'] = "johndoe" // Must be already set
?>
如何在django中编写上述等效代码
答案 0 :(得分:4)
当SessionMiddleware被激活时,每个HttpRequest对象 - 任何Django视图函数的第一个参数 - 将具有session属性,这是一个类似字典的对象。你可以阅读并写信给它。
示例:
def login(request):
m = Member.objects.get(username=request.POST['username'])
if m.password == request.POST['password']:
request.session['member_id'] = m.id
return HttpResponse("You're logged in.")
else:
return HttpResponse("Your username and password didn't match.")