0

After the user logs in i want to redirect to the view ('profile:profile_view')

settings.py

LOGIN_REDIRECT_URL = 'profile:profile_view'

I tried the above code but was not getting the result that i needed because iam passing slug into the url

urls.py

url(r'^(?P<slug>[-\w\d]+)/$', ProfileView.as_view(), name='view_profile'),

from here i tried the following.

settings.py

LOGIN_REDIRECT_URL = 'login_redirect'

urls.py

@login_required
def login_redirect(request):
    return redirect('profile:profile_view', pk=request.user.pk, name=request.user.username)

Now i do get the username in the terminal but how do i use the username for the following 'localhost:8000/username'

views.py

class ProfileView(DetailView):
    template_name = "profile/profile_view.html"
    queryset = User.objects.all()
    context_object_name = 'profile'

What am i doing wrong here? is there a better way?

Community
  • 1
  • 1
Rahul
  • 141
  • 3
  • 16

2 Answers2

0

Your profile URL accepts only one parameter called slug but you're passing it two parameters called pk and name.

First, change your URL like so:

url(r'^(?P<username>[-\w\d]+)/$', ProfileView.as_view(), name='view_profile'),

Now, correct the login_redirect view to make it look like this:

@login_required
def login_redirect(request):
    return redirect('profile:profile_view', username=request.user.username)
xyres
  • 20,487
  • 3
  • 56
  • 85
0

You can also come in login in views.py and check and after that use httpresponseredirect to redirect user: For examplele:

In urls.py :

           From . Import views

           Url(r'$login/',  views.login,  name="login"),
           Url(r'$profile/', viesm.profile, name="profile"),

In views.py:

         From django.shortcuts import get_object_or_404, render
         From django.http import HttpResponseRedirect
          Def login(request):
                  *** place to check if exist in your database***
                  If ***exist***:
                         User=request.post["username"]
                         Request.session["username"]=user
                         Return httpresponseredirect("../profile")
                  Else:
                         Return httpresponseredirect("../login")

        Def profile(request):
                If "user" in request.session:
                        Return render(request," **path to your template  ", { "username" : request.session["username"] })
                else:
                        Return httpresponseredirect("../login")

It should be better i think

Omid Reza Heidari
  • 658
  • 12
  • 27