导读:本篇文章首席CTO笔记来给大家介绍有关django怎么跳转网页的相关内容,希望对大家有所帮助,一起来看看吧。
Django写的LoginView,登录成功后无法跳转回原页面,求助
You do not need to make an extra view for this, the functionality is already built in.
First each page with a login link needs to know the current path, and the easiest way is to add the request context preprosessor to settings.py (the 4 first are default), then the request object will be available in each request:
settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.request",
)
Then add in the template you want the Login link:
base.html:
a href="{% url django.contrib.auth.views.login %}?next={{request.path}}"Login/a
This will add a GET argument to the login page that points back to the current page.
The login template can then be as simple as this:
registration/login.html:
{% block content %}
form method="post" action=""
{{form.as_p}}
input type="submit" value="Login"
/form
{% endblock %}
django本地重定向外网
相对简单,利用HTTP_REFERER,Django的注销页面这样写就行:
def logout_user(request):
logout(request)
return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
登录操作:
登陆操作相对复杂,因为一般都有单独的登陆页面,如果登陆成功再取HTTP_REFERER就是登陆页面自己的url,而不是之前的那个页面。
求教一个关于django的 页面跳转的问题
跳转和重定向很常见的场景就是登录和注销后返回到当前页面。给你个登录和注销的例子。 比如用户正在浏览一篇文章,发现下载该文章的附件需要登录才能进行
结语:以上就是首席CTO笔记为大家整理的关于django怎么跳转网页的全部内容了,感谢您花时间阅读本站内容,希望对您有所帮助,更多关于django怎么跳转网页的相关内容别忘了在本站进行查找喔。