在我的django 1.6项目中添加其他应用程序后,我遇到了django的HttpResponseRedirect函数的一个奇怪问题。
我有两个应用程序的登录视图,但是当我尝试登录到第二个应用程序时,我被重定向到first_app / list而不是second_app / index。
所以在我的第一个应用程序中,视图是
def login(request):
aut_form = auth.forms.AuthenticationForm()
if(request.method == 'POST'):
username = request.POST['username']
password = request.POST['password']
user = auth.authenticate(username=username, password=password)
if user is not None:
if user.is_active:
auth.login(request, user)
return HttpResponseRedirect(reverse('fist_app:list'))
else:
return HttpResponseRedirect(reverse('fist_app:login') + '?loggedout')
else:
return HttpResponseRedirect(reverse('fist_app:login') + '?invalid')
else:
return render(request, 'first_app/login.html', {'form' : aut_form})
def list(request):
if request.user is None or not request.user.is_authenticated():
return HttpResponseRedirect(reverse('fist_app:login'))
else:
return response
并且在第二个应用程序中,views.py看起来几乎相同
def login(request):
aut_form = auth.forms.AuthenticationForm()
if(request.method == 'POST'):
username = request.POST['username']
password = request.POST['password']
user = auth.authenticate(username=username, password=password)
if user is not None:
if user.is_active:
auth.login(request, user)
print("success")
return HttpResponseRedirect(reverse('second_app:index'))
else:
return HttpResponseRedirect(reverse('second_app:login') + '?loggedout')
else:
return HttpResponseRedirect(reverse('second_app:login') + '?invalid')
else:
return render(request, 'second_app/login.html', {'form' : aut_form})
def index(request):
return render(request, 'second_app/index.html')
我还在两个过于模糊的正则表达式应用中检查了我的urls.py.
first_app的urls.py
urlpatterns = patterns('first_app.views',
url(r'^list/$', views.list, name='list' ),
url(r'^login$', views.login, name='login'),
)
第二个应用的urls.py
urlpatterns = patterns('second_app.views',
url(r'^login$', views.login, name='login'),
url(r'^index$', views.index, name="index"),
)
我需要更改哪些内容才能重定向到与登录页面相关的应用中的正确视图?
和我的项目urls.py
urlpatterns = patterns('',
url(r'^backend/', include('second_app.urls', namespace="second_app")),
url(r'^polls/', include('first_app.urls', namespace="first_app")),
url(r'^admin/', include(admin.site.urls)),
)
答案 0 :(得分:0)
查看您共享的代码段后。我可以猜测的唯一可能原因是您没有在表单login
下的模板login.html
中使用特定于应用的action
网址。