Django HttpResponseRedirect vs render_to_response

2019-04-02 22:09发布

问题:

I've already checked out the following stackoverflow question regarding the difference between HttpResponse, HttpResponseRedirect, and render_to_response, as well as having gone through the official django docs, but I'm really uncertain how best to get the functionality I'm looking to create.

Right now I have an index.html with a login function (as seen in the views.py below) where the render_to_response that brings me to portal/index.html. However, as urls.py (see below) dictates, the url in the url bar of my browser is http://127.0.0.1:8000/login/. This means that refreshing the page forces the form to go again.

How do I get that url (once logged in) to look like http://127.0.0.1:8000/ or, if that's not feasible, http://127.0.0.1:8000/portal/ - this is because I think it's clumsy that every time you reload the page once logged in, it forces the browser to open the prompt Are you sure you want to send a form again?.

Thank you so much for helping a Django newbie!

views.py

@cache_page(60 * 15)
def login_user(request):
    #inactive_image_url = ""
    #invalid_credentials_url = ""
    context_instance=RequestContext(request)
    if request.POST:
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)            
                state = "You're successfully logged in!"
                return render_to_response('ucproject/portal/index.html', 
                        {'state':state, 'username':username}, context_instance=RequestContext(request))
            else:
                #state_img = inactive_image_url
                state = "Your account is not active, please contact UC admin."
        else:
            #state_img = invalid_credentials_url
            state = "Your username and/or password were incorrect."
    return render_to_response('ucproject/index.html', 
            {'state': state,
             #'state_img': state_img,
             'username': username
            }, context_instance=RequestContext(request))

def portal(request):
    username = 'username'
    return render_to_response('ucproject/portal/index.html', 
            {'state': state,'username': username}, context_instance=RequestContext(request))

urls.py

# Login / logout.
url(r'^registration/$', 'portal.views.registration'),
url(r'^login/$', 'portal.views.login_user'),
url(r'^portal/$', 'portal.views.portal'),
url(r'^portal/index.html$', 'portal.views.portal'),

回答1:

Replace the HttpResponse here with a redirect:

if user.is_active:
    login(request, user)            
    state = "You're successfully logged in!"
    return render_to_response('ucproject/portal/index.html', 
           {'state':state, 'username':username}, context_instance=RequestContext(request))

Instead, you want to use a name in urls.py to disambiguate:

urls.py
url(r'^portal/$', 'portal.views.portal', name='home'),
url(r'^portal/index.html$', 'portal.views.portal', name='home_index'),

then use something like this in your view:

if user.is_active:
    login(request, user)
    return redirect('home')

redirect is a shortcut function, but you could also create and return an HttpResponseRedirect object. Usually there's no point in doing that.