[Answer]-Invalid literal for int() with base 10: 'abc12345'

1👍

You are using standart user, so User pk is always int, if you want to select user by username you have to rewrite get_object method of your view, like this:

class UserDetailView(DetailView):

def get_object(self, queryset=None):
    if queryset is None:
        queryset = self.get_queryset()
    username = self.kwargs.get('username', None)
    return queryset.get(username=username)

and add username to url:

url(r"^(?P<username>\w+)/$", UserDetailView.as_view(), name="test.html"),
👤zymud

0👍

Even without knowing regex, it should be clear to you that you haven’t got a URL matching “/abc12345/”, you only have one matching “/user/12345”. You’ve hard-coded that “user” prefix.

The other problem is that your regex only accepts digits (\d+) rather than alphanumeric characters (\w+).

Leave a comment