我在我的网页中添加了django.contrib.auth.views.login ,因为我必须在base.html中加载templatetag(返回AuthenticationForm)。此模板标记包含registration/login.html
模板。
登录工作正常但我希望它在登录前将用户重定向到他们所在的页面。现在,它将我重定向到/ wherever_i_am / login,显示registration/login.html
,其中包含'login ok'或'login failed'消息,但没有base.html的其余部分。
我已经关注了django文档和一些SO问题,例如this,但我无法正确重定向。我修改了next
变量,但它似乎不起作用(next={{ request.get_full_path }}
将我重定向到/ wherever_i_am / login ...再次)
你尝试过类似的东西吗?任何想法?
UPDATE1 现在,问题可能是这样的:如果我想在我的网页中包含登录表单,我是否必须声明自己的登录视图?
谢谢。
答案 0 :(得分:14)
找到答案:
更改settings.py中的settings.LOGIN_REDIRECT_URL,
下面的代码是来自django:
的副本 if request.method == "POST":
form = authentication_form(data=request.POST)
if form.is_valid():
# Ensure the user-originating redirection url is safe.
if not is_safe_url(url=redirect_to, host=request.get_host()):
redirect_to = settings.LOGIN_REDIRECT_URL
...
答案 1 :(得分:6)
以下内容允许用户在登录后将其重定向到他们尝试访问的页面,但无需编写自定义视图。它包含您需要添加的所有代码以使其工作。 (顺便说一句,并不是所有的TEMPLATE_CONTEXT_PROCESSORS都需要,但是如果你明确地设置了一个值,你就会覆盖默认值,所以需要重新添加它们。)
<强> settings.py 强>
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.request",
"django.core.context_processors.static",
)
<强> urls.py 强>
from django.contrib.auth.views import login, logout
...the other imports for your app ...
urlpatterns = patterns('',
(r'^login/$', login, {'template_name':'login.html'} ),
(r'^logout/$', logout,{'template_name':'logout.html'}),
...the other urls for your app...
)
<强>的login.html 强>
<html>
<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}
{{form}}<br/>
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
</html>
<强> logout.html 强>
<html>
<p>You are logged out. To log in again, click <a href="/login/">here</a>.</p>
</html>
<强> views.py 强>
@login_required(login_url="/login/")
def view1(request):
....this is a view you want to protect with security...
@login_required(login_url="/login/")
def view1(request):
....this is a view you want to protect with security...
答案 2 :(得分:2)
我在默认登录视图中使用了类似的东西:
{% if form.errors %}
<p class="error">Sorry, that's not a valid username or password</p>
{% endif %}
<form action="{% url login %}" method="post">
{% csrf_token%}
<label for="username">User name:</label>
<input type="text" name="username" value="" id="username">
<label for="password">Password:</label>
<input type="password" name="password" value="" id="password">
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ request.get_full_path }}" />
</form>
# or if it's not declareв шт urls:
<form action="{% url django.contrib.auth.views.login %}?next={{ request.get_full_path }}" method="post">
一切顺利。
PS:你绝对确定“context_processors.request”包含在设置中吗?忘记包含它是一个常见的问题。
UPD :据我所知,没有办法让默认登录视图在登录失败时重定向(它不能正常工作)。
我可能还是错了
答案 3 :(得分:2)
最后,我创建了一个在内部调用django.contrib.auth.views.login的登录视图。
答案 4 :(得分:0)
我建议将以前的url作为url中的参数传递:
/accounts/login/?next=my_previous_url
然后在视图中使用此值
request.next
答案 5 :(得分:0)
{{request.get_full_path}}
为您提供当前路径,因此重定向指向同一位置是正常的,在{{next}}
模板中将其更改为registration/login.html
答案 6 :(得分:0)
加起来@Sean's anwer。迭代每个表单字段的代码,用于在未命中字段上方写入字段错误。
因此,在Sean的login.html中是现有代码:
<强>的login.html 强>
<html>
<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}
{{form}}<br/> <!-- I can change! -->
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
</html>
现在你要做的就是替换&#34;我可以改变!&#34; line(上面代码段中的第4行),代码如下:
{% for field in form %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<span class="text-danger small"> {{ field.errors }}</span>
</div>
<label class="control-label col-sm-2">{{ field.label_tag }}</label>
<div class="col-sm-10"> {{ field }}</div>
</div>
{% endfor %}
您也可以将此代码段用于其他表单(例如注册)。 :)
答案 7 :(得分:0)
在实施Facebook帐户链接的过程中,我偶然发现了这个问题。问题是一样的:成功登录后如何正确重定向django?
请记住:您的settings.py包含LOGIN_REDIRECT_URL对吗?因此,这是您应该执行重定向逻辑的唯一位置。为此,请先连接此信号(将此信号放入您的views.py):
def after_success_login(sender, user, request, **kwargs):
alt = request.GET.get('account_linking_token')
if alt is not None:
uri = request.GET.get('redirect_uri')
request.session['fb_redirect_uri'] = uri
user_logged_in.connect(after_success_login)
以上逻辑可能无法反映您的情况,但想法是设置一个会话变量以在定义为LOGIN_REDIRECT_URL的路由中读取。
所以,就我而言:
def index(request):
if not request.user.is_authenticated():
form = SignUpForm()
return render(request, 'index.html', {'form': form})
else:
# FB ACCOUNT LINKING!
if 'fb_redirect_uri' in request.session:
redirect_uri = request.session['fb_redirect_uri']
del request.session['fb_redirect_uri']
to = '{}&authorization_code={}'.format(redirect_uri, request.user.username)
print('to', to)
return redirect(to)
就是这样!
答案 8 :(得分:-5)