简体   繁体   中英

Django redirecting to wrong view in other app

After adding an other app to my django 1.6 project I ran into a strange problem with the HttpResponseRedirect function of django.

I have a login view for both of the apps but when I try to login to the second app I got redirected to first_app/list instead to second_app/index.

so in my first app the view is

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

and in the second app the views.py looks pretty much the same

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')

I also checked my urls.py in both apps of regular expressions that are too vague.

urls.py of first_app

urlpatterns = patterns('first_app.views',
    url(r'^list/$', views.list, name='list' ),
    url(r'^login$', views.login, name='login'),
)

urls.py of second app

urlpatterns = patterns('second_app.views',
   url(r'^login$', views.login, name='login'),
   url(r'^index$', views.index, name="index"),
)

What do I have to change to get redirected to the correct view in the login-page related app?

and my projects 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)),
)

After looking at code snippets you have shared. The only possible reason I can guess is that you are not using app specific login URL in template login.html under form action .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM