简体   繁体   中英

How to Call default home url in django when i logged out

Please visit this link for getting whole idea behind this question

How to Call loggedin username in Django url

Here i have discussed my points in this link but i didnt got specific answer for my issue that , when user loggedin i wanted it to be displayed in my url as " 127.0.0.1:8000/username " as i got the solution in above link as create user defind HomeRedirectView which calls initially when user logsin. and it works successfully, but i got an issue when i logged out and revisit the url as " 127.0.0.1:8000/ " then this url automatically becomes " 127.0.0.1:8000/AnonymousUser " and am getting the error as "NoReverseMatch", for that i have to specifically write it into url as " 127.0.0.1:8000/home/ " then it works. So can any one suggest me how to make url as " 127.0.0.1:8000/home/ ". To know about what i have done uptill now ,please visit above link and you will come to know from the discussion.

Please suggest.

The solution you got there is not the right solution, the right solution is to use the LOGIN_REDIRECT_URL setting and point it to a view function, a named URL pattern or a direct URL.

Once a user is logged in using the default authentication mechanism of django, the request will automatically be redirected to this page.

Your second problem is when you logout a user, you want to be redirected to a specific URL. If you use the correct solution above, then all you need to do is:

  1. Set LOGOUT_URL in your settings.py .

  2. Create your logout view, it can be as simple as this example from the documentation :

     from django.shortcuts import redirect from django.contrib.auth import logout def logout_view(request): logout(request) return redirect('/home/') 

If you want to stick with your original solution, then modify it like this:

class HomeRedirectView(RedirectView):
    pattern_name = 'home'

    def get_redirect_url(self, *args, **kwargs):
        if self.request.user.is_authenticated():
            return "/user/{}/".format(self.request.user)
        else:
            return '/home/'

I think you are overcomplicating things a little, the following will allow you to redirect to a user home page if a user is logged in, or it will display an un-logged in view. I have made the assumption that the username in the URL is purely for display purposes (otherwise it could be a security issue for your application.

urls.py

urlpatterns = patterns('myapp.views',
   url(r'^/$', 'home', name='home'),
   url(r'^user/[-_.\w\d]+/$', 'user_home', name='user-home'),
)

views.py

from django.contrib.auth.models import User
from django.shortcuts import redirect, render, get_object_or_404


def home(request):
    """
    Home page
    """
    # If a user is authenticated then redirect them to the user page
    if request.user.is_authenticated:
        return redirect('user-home', request.user.username)
    else:
        return render(request, "myapp/home.html")

@login_required
def user_home(request):
    """
    User specific home page, assume the username in URL is just for decoration.
    """
    return render(request, "mpapp/home_user.html", {
        "user": request.user
    }

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