Python Django重定向到其他应用程序中的错误视图

Python Django重定向到其他应用程序中的错误视图,python,django,Python,Django,在我的django 1.6项目中添加了另一个应用程序之后,django的HttpResponseRedirect函数遇到了一个奇怪的问题 我有两个应用的登录视图,但当我尝试登录到第二个应用时,我被重定向到第一个应用/列表,而不是第二个应用/索引 因此,在我的第一个应用程序中,视图是 def login(request): aut_form = auth.forms.AuthenticationForm() if(request.method == 'POST'): username =

在我的django 1.6项目中添加了另一个应用程序之后,django的HttpResponseRedirect函数遇到了一个奇怪的问题

我有两个应用的登录视图,但当我尝试登录到第二个应用时,我被重定向到第一个应用/列表,而不是第二个应用/索引

因此,在我的第一个应用程序中,视图是

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')
我还检查了两个正则表达式应用程序中过于模糊的url.py

第一个应用程序的url.py

urlpatterns = patterns('first_app.views',
    url(r'^list/$', views.list, name='list' ),
    url(r'^login$', views.login, name='login'),
)
第二个应用程序的url.py

urlpatterns = patterns('second_app.views',
   url(r'^login$', views.login, name='login'),
   url(r'^index$', views.index, name="index"),
)
要重定向到登录页面相关应用程序中的正确视图,我必须更改什么

还有我的项目url.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)),
)

查看您共享的代码片段后。我能猜到的唯一可能的原因是,您没有在form action下的template login.html中使用特定于应用程序的登录URL。

显示包含应用程序URL的主URL配置我认为您总是在第一个视图中结束,除非您的登录模式有前缀,因为我怀疑,它是URL.py中的第一个。我已经将项目URL.py添加到问题字段中。不幸的是,我已经尝试在global url.py文件中更改应用程序顺序以避免此错误。还有其他解决方法吗?我想问题出在login.html中。你是如何在表单操作中添加URL的?啊,你知道了。在我将模板文件从第一个应用程序复制到第二个应用程序后,我一定忘了更改登录表单中的操作。您可以将此作为答案发布。