这里我正在尝试设置会话变量guest_email_id并尝试打印它们。
def guest_register_view(request):
form = GuestForm(request.POST or None)
next_get = request.GET.get("next")
next_post = request.POST.get("next")
redirect_path = next_get or next_post or None
if form.is_valid():
email = form.cleaned_data.get("email")
new_guest_user = GuestEmail.objects.create(email=email)
request.session["guest_email_id"] = new_guest_user.id
print(new_guest_user.id)
print(request.session.get('guest_emial_id'))
if is_safe_url(redirect_path, request.get_host()):
# print(redirect_path)
return redirect(redirect_path)
else:
return redirect("register")
context = {
"form" : form
}
return render(request, "accounts/register.html", context)
此视图的网址:
path('register/guest/', guest_register_view, name="guest_register"),
我能够创建仅具有一个字段(电子邮件)的GuestEmail对象并获取其ID,但是我没有获得我要设置的会话变量的值。
我得到以下输出:
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
November 09, 2019 - 10:29:52
Django version 2.2.6, using settings 'ecommerce.settings'
Starting development server at http://0:8000/
Quit the server with CONTROL-C.
[09/Nov/2019 10:30:02] "GET /register/guest/ HTTP/1.1" 200 2800
21
None
[09/Nov/2019 10:30:05] "POST /register/guest/ HTTP/1.1" 302 0
[09/Nov/2019 10:30:05] "GET /register/ HTTP/1.1" 200 3283
有人可以帮助我解决代码中的错误吗?
It's just a spelling mistake of your variable - "guest_email_id" while printing the session variable.
request.session["guest_email_id"] = new_guest_user.id
print(new_guest_user.id)
print(request.session.get('guest_email_id'))