Search code examples
djangodjango-viewsdjango-users

User matching query does not exist - django


I have a page which shows the user and their about. And in that, there's a link to update their about. But when I open that link it shows me with this error:

DoesNotExist at /profile/user/update_about/

User matching query does not exist.

And the traceback hightlights this line, which from the profile method in the views:

13.  user = User.objects.get(username=unquote(user_name)) 

However this error does not occur when I load the profile method. It occurs only on the update_profile method in the views.

views.py

from django.shortcuts import render
from django.http import HttpResponseRedirect
from urllib import unquote

from django.contrib.auth.models import User

from models import About
from forms import AboutForm
# Create your views here.


def profile(request, user_name):
    user = User.objects.get(username=unquote(user_name))
    about = About.objects.get_or_create(user=user)
    about = about[0]

    return render(request, 'user_profile.html', {
        'user':user,
        'about_user':about
    })

def update_about(request, user_name):
    user = User.objects.get(username=unquote(user_name))
    if request.method == 'POST':
        form = AboutForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/')
    else:
        about = About.objects.get(user=user)
        form = AboutForm(initial={'dob':about.dob})
        return render(request, 'update_about.html',{
            'form':form
        })

urls.py

urlpatterns = patterns('',
    # Examples:
    url(r'(?P<user_name>[\w@%.]+)/$', 'user_related.views.profile', name='profile'),
    url(r'(?P<user_name>[\w@%.]+)/update_about/$', 'user_related.views.update_about', name='update_about'),

What is causing this? Your help will be very much appreciated. Thank you.


Solution

  • You forgot to add the caret sign (^) at the first position of regex. So the first regex matched "update_about/" part of the url.

    Fixed code:

    url(r'^(?P<user_name>[\w@%.]+)/$', 'user_related.views.profile', name='profile'),
    url(r'^(?P<user_name>[\w@%.]+)/update_about/$', 'user_related.views.update_about', name='update_about'),