Jam*_*mes 31 python django next
我有一个正常工作的登录页面,但重定向到引用页面除外.用户在应用程序中收到带有直接链接的电子邮件,他们(在此示例中)尚未登录并被重定向到登录页面.成功登录后,用户将被重定向到硬编码路径.见下面的例子.
电子邮件中的URL: http://localhost:8000/issueapp/1628/view/22
登录页面的URL: http://localhost:8000/login?next=/issueapp/1628/view/22
登录视图(带硬编码重定向):
def login_user(request):
state = "Please log in below..."
username = password = ''
if request.POST:
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
state = "You're successfully logged in!"
return HttpResponseRedirect('/issueapp/1628/')
else:
state = "Your account is not active, please contact the site admin."
else:
state = "Your username and/or password were incorrect."
return render_to_response(
'account_login.html',
{
'state':state,
'username': username
},
context_instance=RequestContext(request)
)
Run Code Online (Sandbox Code Playgroud)
登录视图(带有"下一步"重定向):
def login_user(request):
state = "Please log in below..."
username = password = ''
if request.POST:
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
state = "You're successfully logged in!"
return HttpResponseRedirect(request.GET['next'])
else:
state = "Your account is not active, please contact the site admin."
else:
state = "Your username and/or password were incorrect."
return render_to_response(
'account_login.html',
{
'state':state,
'username': username
},
context_instance=RequestContext(request)
)
Run Code Online (Sandbox Code Playgroud)
上面的视图导致异常"Key 'next' not found in <QueryDict: {}>"表单似乎没有发布"下一个"变量,即使它位于url和表单中.我搜索过,到处寻找,无法弄清楚为什么它不起作用.有任何想法吗?
附加参考:
登录模板:
{% block content %}
{{ state }}
<form action="/login/" method="post" >
{% csrf_token %}
{% if next %}
<input type="hidden" name="next" value="{{ next }}" />
{% endif %}
username:
<input type="text" name="username" value="{{ username }}" /><br />
password:
<input type="password" name="password" value="" /><br />
<input type="submit" value="Log In"/>
{% debug %}
</form>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
编辑:以下是现在为我工作的代码(感谢Paulo Bu的帮助)!**
登录视图:
def login_user(request):
state = "Please log in below..."
username = password = ''
next = ""
if request.GET:
next = request.GET['next']
if request.POST:
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
state = "You're successfully logged in!"
if next == "":
return HttpResponseRedirect('/issueapp/')
else:
return HttpResponseRedirect(next)
else:
state = "Your account is not active, please contact the site admin."
else:
state = "Your username and/or password were incorrect."
return render_to_response(
'account_login.html',
{
'state':state,
'username': username,
'next':next,
},
context_instance=RequestContext(request)
)
Run Code Online (Sandbox Code Playgroud)
登录模板:
{{ state }}
{% if next %}
<form action="/login/?next={{next}}" method="post" >
{%else%}
<form action="/login/" method="post" >
{% endif %}
{% csrf_token %}
username:
<input type="text" name="username" value="{{ username }}" /><br />
password:
<input type="password" name="password" value="" /><br />
<input type="submit" value="Log In"/>
{% debug %}
</form>
Run Code Online (Sandbox Code Playgroud)
Pau*_* Bu 34
您的代码很好,唯一的问题是在表单中您将next属性作为帖子传递,因为方法是post.在视图中,您尝试获取字典中的next参数,get这显然不存在.
您必须声明这样的html表单action才能使您的视图正常工作.
{% if next %}
<form action="/login/?next={{next}}" method="post" >
{%else%}
<form action="/login/" method="post" >
{% endif %}
{% csrf_token %}
username:
<input type="text" name="username" value="{{ username }}" /><br />
password:
<input type="password" name="password" value="" /><br />
<input type="submit" value="Log In"/>
{% debug %}
</form>
Run Code Online (Sandbox Code Playgroud)
在那里,如果有一个next变量,那么你在urlfor中包含它作为get参数检索它.如果没有,表格不包括它.
这是最好的方法,但您也可以通过next从POST字典中请求以下内容来修复此视图:
return HttpResponseRedirect(request.POST.get('next'))
Run Code Online (Sandbox Code Playgroud)
请注意,这仅在模板account_login 具有调用的变量时才有效next.您应该在视图中生成它并在渲染时将其传递给模板.
通常,在模板中你会做这样的事情:
# this would be hardcoded
next = '/issueapp/1628/view/22'
# you may add some logic to generate it as you need.
Run Code Online (Sandbox Code Playgroud)
然后你做:
return render_to_response(
'account_login.html',
{
'state':state,
'username': username,
'next':next
},
context_instance=RequestContext(request)
)
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
我会在你的视图函数中定义next_page = request.GET['next']然后重定向到它,return HttpResponseRedirect(next_page)所以你永远不需要更改模板; 刚设置@login_required,你很好.
用户A尝试访问 - 但未登录 - https://www.domain.tld/account/.Django重定向他,因为@login_required设置为LOGIN_URLsettings.py中定义的.如果用户A成功登录,该方法UserLogin现在尝试参数并重定向到GET该next参数.
LOGIN_URL = '/login/'
Run Code Online (Sandbox Code Playgroud)
url(r'^account/', account, name='account'),
url(r'^login/$', UserLogin, name='login'),
Run Code Online (Sandbox Code Playgroud)
@login_required
def account(request):
return HttpResponseRedirect("https://www.domain.tld/example-redirect/")
def UserLogin(request):
next_page = request.GET['next']
if request.user.is_authenticated():
return HttpResponseRedirect(next_page)
else:
if request.method == 'POST':
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(email=username, password=password)
if user is not None and user.is_active:
login(request, user)
return HttpResponseRedirect(next_page)
else:
error_msg = 'There was an error!'
return render(request, "login", {'form': form, 'error_msg': error_msg})
else:
error_msg = "There was an error!"
return render(request, "login", {'form':form, 'error_msg':error_msg})
else:
form = UserLoginForm()
return render(request, "login", {'form': form})
Run Code Online (Sandbox Code Playgroud)
就放
<form action="" method="post" >
Run Code Online (Sandbox Code Playgroud)
空动作' what ever current complete url is'
| 归档时间: |
|
| 查看次数: |
51095 次 |
| 最近记录: |