2

Till now i have been using userid's (pk) as an argument in the profile urls for my application. Recently i decided to use the username instead of the user id's So i changed the following :

Urls.py

# This was what i was using previously
url(r'^profile/(?P<uid>\d+)/$', 'app.views.user_profile', name="profile"),

# Changed the above to this.
url(r'^profile/(?P<username>[-\w\d]+)/$', 'app.views.user_profile', name="profile"),

Views.py

# Previous view
@login_required
def user_profile(request, uid):

    user = get_object_or_404(models.User, pk=uid)
    profile = helpers.get_profile(user)
    render_to_response('profile.html', {'profile' : profile})


# Changed the above to
@login_required
def user_profile(request, username):

    user = get_object_or_404(models.User, username=username)
    profile = helpers.get_profile(user)
    render_to_response('profile.html', {'profile' : profile})

Until Now it's all good. When i try to visit 127.0.0.1:8000/profile/some_username/ instead of the previous 127.0.0.1:8000/profile/some_number/ it works fine.

I have a toolbar in the profile.html, which contains links such as:

/profile/edit/
/profile/settings/

in the urls.py these urls map to very simple views. Such as the following.

@login_required
def profile_settings(request):
    """  
    This view helpers.renders the profile's settings panel
    """
    print 'hello'
    rec, can = helpers.get_user_or_noprofile(request.user)
    if not can is None:
        gform = forms.CandidateGeneralSettingsForm(instance=can)
        jsform = forms.CandidateJobSearchSettingsForm(instance=can)
        data = {'candidate' : can, 'gform' : gform, 'jsform' : jsform }
    else:
        form = forms.RecruiterSettingsForm(instance=rec)
        data = { 'recruiter' : rec, 'form' : form }
    return helpers.render(request, 'profile_settings.html', data)

The weird part is after i changed to use username in the profile url. As soon as i click on any of the links in the toolbar, i see the 404 : Error : No User matches the given query. page.

To debug i tried printing a few debug statements within these views and found out something more weird. Nothing gets printed even if i Comment the whole code inside the view and just write a print statement.

This makes me doubt that the problem might be with the login_required decorator. but if that's the case howcome it is working while visitng 127.0.0.1:8000/profile/some_username/.

I really can't think of anything that might be causing this. Any help will be greatly appreciated. Thanks.

PS: Also, JFYI, i am using a custom Email Authentication backend that lets user login through their email address instead of the username.

Amyth
  • 32,527
  • 26
  • 93
  • 135

1 Answers1

4

It doesn't work because edit is not a valid username.

This url /profile/edit/ matches ^profile/(?P<username>[-\w\d]+)/$, and username becomes edit.

Since you don't have a user called "edit", this user = get_object_or_404(models.User, username=username) fails, and raises a 404.

The same for /profile/settings/.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284