jeremy_lord
jeremy_lord

Reputation: 469

Why are usernames with uppercase letters causing errors Django

I have a website where users are able to sign up and friend each other. However, I have noticed that users with uppercase letters in their names are causing errors when users try to friend them. It just comes up with a 404 page, and I have figured out why this is, just not how to fix it.

models.py

class UserProfileInfo(models.Model):
    MALE = 'Male'
    FEMALE = 'Female'

    GENDER = [
        (MALE, 'Male'),
        (FEMALE, 'Female'),
    ]

    user = models.OneToOneField(User, on_delete=models.CASCADE,max_length=30)
    description = models.TextField(max_length=150)
    slug = models.SlugField(unique=True,allow_unicode=True)
    def __str__(self):
        return f'{self.user.username}'

    def get_absolute_url(self):
        kwargs = {
            'username':self.user.username,
        }
        return reverse('mainapp:view_profile_with_pk',kwargs=kwargs)
        # return reverse('mainapp:post_list')

    def save(self, *args, **kwargs):
        # slugl = self.slug.lower()
        self.slug = slugify(self.user.username)
        super().save(*args, **kwargs)

And my views for adding friends

class AddFriendRedirect(RedirectView):
    def get_redirect_url(self,*args,**kwargs):
        username = self.kwargs.get("username")
        obj = get_object_or_404(UserProfileInfo,slug=username)
        # print(f'OBJECT: {other}')
        # print(f'Current user: {self.request.user}')
        # user_profile = User.objects.get(username=username)
        url_ = obj.get_absolute_url()
        user = self.request.user
        user_ = self.request.user.username
        # user__ = 
        if user.is_authenticated:
            print("User is authenticated")
            if user in obj.friends.all():
                obj.friends.remove(user)
                obj.save()
            else:
                obj.friends.add(user)
                obj.save()

        return url_

And finally the url pattern

    # path('profile/<str:username>/add/',views.AddFriendRedirect.as_view(),name='add_friend'),
    re_path(r'^profile/(?P<username>[\w.@+-]+)/add/$(?i)',views.AddFriendRedirect.as_view(),name='add_friend'),

And now for the users with capital letters in their names.

The reason for this I have come to realise is that the slug is in lowercases, but I can't figure out why I am getting this 404 error.

Their profile page, which path is path('profile/<str:username>/',views.view_profile,name='view_profile_with_pk'), works just fine, with no errors. I am stumped as to why this isn't working.

Upvotes: 1

Views: 261

Answers (1)

weAreStarsDust
weAreStarsDust

Reputation: 2752

I think problem is here

obj = get_object_or_404(UserProfileInfo,slug=username)

Change it on

obj = get_object_or_404(UserProfileInfo,user__username=username)

Your slug saved in lowercase here self.slug = slugify(self.user.username) so when you trying to get UserProfileInfo with slug=username and username has uppercase letters it throw 404.

Upvotes: 1

Related Questions