将变量从一个函数移动到另一个函数(在Django用户注册中),python
我对这个很陌生,我已经试了几个小时了,但它不起作用。如果你能帮助我,我将非常感激。非常感谢你 这是我的密码:将变量从一个函数移动到另一个函数(在Django用户注册中),python,python,django,variables,global,Python,Django,Variables,Global,我对这个很陌生,我已经试了几个小时了,但它不起作用。如果你能帮助我,我将非常感激。非常感谢你 这是我的密码: def login_view(request): username1 = request.POST['username'] password1 = request.POST['password'] user = auth.authenticate(username=username1, password=password1) if user is not
def login_view(request):
username1 = request.POST['username']
password1 = request.POST['password']
user = auth.authenticate(username=username1, password=password1)
if user is not None and user.is_active:
# Correct password, and the user is marked "active"
auth.login(request, user)
# Redirect to a success page.
return HttpResponseRedirect("/account/loggedin/")
else:
# Show an error page
return HttpResponseRedirect("/account/invalid/")
def profile_page(request):
html = loader.get_template('profile_test.html')
login_view(request)
c = Context({'user' : username1})
return HttpResponse(html.render(c))
当我把username1放在profile_页面的引号中时,一切都很完美。但是,当我使用username1作为全局变量时,它不起作用。如何使用username1作为全局变量?非常感谢你,我已经试了好几个小时了,但什么都没用。再次感谢你 由于没有返回HttpResponse,因此login\u view调用不会执行任何操作 如果您的用户已登录,则任何请求都应自动包含该用户。例如:
if request.user.is_authenticated():
user = request.user
context['user'] = user.username
否则,您可以在url中传递用户,然后在视图中获取用户名(或者只使用模板中的user对象,并在模板中插入{{user.username}})