gpt4 book ai didi

python - Django应用部署在suburl,登录后重定向到首页

转载 作者:太空宇宙 更新时间:2023-11-03 12:05:57 24 4
gpt4 key购买 nike

我的页面部署在 http://example.com。我还在 http://example.com/djangoapp 部署了我的 Django 应用程序。

我正在使用 Apache 2.2 和此配置 (/etc/apache2/apache2.conf):WSGIPythonPath/home/brian/djangoprojects/djangoapp.

我还在默认的 Apache Virtual Host 文件中添加了 WSGIScriptAlias/djangoapp/home/brian/djangoprojects/djangoapp/djangoapp/wsgi.py 行,它确实有效不错。

但是,在登录后,我的应用程序将我重定向到 http://example.com/ 而不是 http://example.com/djangoapp/homeit

这是我的 urls.py 文件:

from django.conf.urls import include, url
from django.contrib import admin
from djangoapp import views

urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'django.contrib.auth.views.login', name="login"),
url(r'^logout/$', views.logout_page, name="logout"),
url(r'^accounts/login/$', 'django.contrib.auth.views.login', name="login"),
url(r'^register/$', views.register, name="register"),
url(r'^register/success/$', views.register_success, name="register_success"),
url(r'^homeit/$', views.homeit, name="homeit"),
]

这是我的 views.py 文件:

from django.contrib.auth.decorators import login_required
from django.contrib.auth import logout
from django.contrib.auth.models import User
from django.views.decorators.csrf import csrf_protect
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.core.urlresolvers import reverse
from djangoapp.forms import RegistrationForm


@csrf_protect
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = User.objects.create_user(
username=form.cleaned_data['username'],
password=form.cleaned_data['password1'],
email=form.cleaned_data['email']
)
return HttpResponseRedirect(reverse('register_success'))
else:
form = RegistrationForm()
variables = RequestContext(request, {
'form': form
})

return render_to_response(
'registration/register.html',
variables,
)


def register_success(request):
return render_to_response('registration/success.html')


def logout_page(request):
logout(request)
return HttpResponseRedirect(reverse('login'))


@login_required
def homeit(request):
return render_to_response(('home.html', {'user': request.user}))

我的设置文件,settings.py:

LOGIN_URL = '/djangoapp/accounts/login/'
USE_X_FORWARDED_HOST = True
SUB_SITE = "/djangoapp"

最后,我用来登录的登录页面:

{% extends "base.html" %}
{% block title %}Login{% endblock %}
{% block head %}Login{% endblock %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form method="post" action=".">{% csrf_token %}
<table border="0">
<tr><th><label for="id_username">Username:</label></th><td>{{ form.username }}</td></tr>
<tr><th><label for="id_password">Password:</label></th><td>{{ form.password }}</td></tr>
</table>
<input type="submit" value="Login" />
<input type="hidden" name="next" value="{% url "homeit" %}" />
</form>
<a href="{% url "register" %}">Register</a>
{% endblock %}

最佳答案

快速阅读

解决方法:

  • 在settings.py中添加LOGIN_REDIRECT_URL = '/djangoapp/homeit'
  • urls.py9 更改为 url(r'^accounts/login/$', 'django.contrib.auth.views.login ', {'extra_context': {'next':'/djangoapp/homeit'}}),

解释

检查 django.contrib.auth.views.login 的文档 here

def login的代码如下:

def login(request, template_name='registration/login.html',
redirect_field_name=REDIRECT_FIELD_NAME,
authentication_form=AuthenticationForm,
current_app=None, extra_context=None):
"""
Displays the login form and handles the login action.
"""
redirect_to = request.POST.get(redirect_field_name,
request.GET.get(redirect_field_name, ''))

if request.method == "POST":
form = authentication_form(request, 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 = resolve_url(settings.LOGIN_REDIRECT_URL)

# Okay, security check complete. Log the user in.
auth_login(request, form.get_user())

return HttpResponseRedirect(redirect_to)
else:
form = authentication_form(request)

current_site = get_current_site(request)

context = {
'form': form,
redirect_field_name: redirect_to,
'site': current_site,
'site_name': current_site.name,
}
if extra_context is not None:
context.update(extra_context)

if current_app is not None:
request.current_app = current_app

return TemplateResponse(request, template_name, context)

发生了什么:

  • django.contrib.auth.views.login 采用 redirect_field_name 并相应地重定向
  • redirect_field_name 的默认值为 next
  • 目前,由于您没有将任何内容作为 next 参数传递,因此它会自动生成 redirect_to = ''
  • HttpResponseRedirect 被调用为 HttpResponseRedirect('')
  • 因此它最终重定向到您的主页,而不是 /djangoapp

关于python - Django应用部署在suburl,登录后重定向到首页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32385438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com