2

I have an app which allows users to create a profile and log in.

When a user login , he is redirected to 127.0.0.1:8000/profile/

The problem is , I want to customize the URL by adding the user's username to the end of URL e.g example 127.0.0.1:8000/profile/michael

This is a similar question to mine

Django - after login, redirect user to his custom page --> mysite.com/username

"get the username and then do a HttpResponseRedirect to the custom URL."

I just can't seem to figure out how could I pass a username as an argument for HttpResponseRedirect to process into a the custom URL properly.

return HttpResponseRedirect('/profile/?username=%s' % (username, request.path))


def Profile(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('world:LoginRequest'))
    person = Person.objects.get(user=request.user)
    return render(request,'profile.html',{'person':person})

my URL

url(
    r'^profile/$',
    'pet.views.Profile',
    name = 'Profile'
),

NEW

my views.py

 def LoginRequest(request):
if request.user.is_authenticated():
        username = User.objects.get(username=request.user)
        url = reverse('Profile', kwargs = {'username': username.username})
        return HttpResponseRedirect(url)        
if request.method == "POST":
    form = LoginForm(request.POST)
    if form.is_valid():
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        Person = authenticate(username=username, password=password)
        if Person is not None:
            login(request, Person)
            username= User.objects.get(username=request.user)

                            url = reverse('Profile', kwargs = {'username': username.username})
                            return HttpResponseRedirect(url)


return render(request, 'login.html',{'form': LoginForm()})




url(
    r'^login/$',
    'pet.views.LoginRequest',
    name = 'LoginRequest'
),

url(
    r'^profile/(?P<username>\w+)/$',
    'pet.views.Profile',
    name = 'Profile'
),


def Profile(request,username):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('world:LoginRequest'))
    board = Board.objects.filter(user=request.user)

    person = Person.objects.get(user__username=username)
    return render(request,'profile.html',{'board':board ,'person':person})
0

2 Answers 2

4

This would be the proper regex for your redirect URL, ie. don't modify the one you have.

url(r'^profile/(?P<username>\w+)/$', 'pet.views.myprofileview', name="detail_profile")

And then to pass an argument to the redirect:

url = reverse('detail_profile', kwargs={'username': profile.firstname})
return HttpResponseRedirect(url)

This leads to also having to define a new view:

def myprofileview(request, username):
    person = Person.objects.get(user = request.user)
    return render(request,'profile.html',{'person':person})

This would eliminate two behaviours in one view, which I find to be very nice!

We do it this way because it's a string that HttpResponseRedirect accepts so we have to build it accordingly.

This will make a redirect to the myprofileview view and "style", if you could call it that, your url /profile/michael/.

Sign up to request clarification or add additional context in comments.

2 Comments

You're formatting it wrong. You should also do this outside the return, it goes for better readability. formatted_string = "/profile/?{0}={1}".format(username, request.path) and then return HttpResponseRedirect(formatted_string).
1
def LoginRequest(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse('world:Profile', 
            kwargs={'username': request.user.username}))  

    if request.method == "POST":
        form = LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']

            Person = authenticate(username=username, password=password)
            if Person is not None:
                login(request, Person)

                return HttpResponseRedirect(reverse('world:Profile', 
                    kwargs={'username': username})) 

    return render(request, 'login.html',{'form': LoginForm()})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.