'How to get user object based on username and not the (id, pk) in Django

I am having struggles viewing other profiles with the users username in the URL, I am able to see there pages with the users ID, but not with there usernames, this is the url now http://127.0.0.1:8000/user/30/, but I want to have this http://127.0.0.1:8000/user/reedlyons/. I know I could do it with the get_object_or_404, but I was wondering if there is another way around that.

Here is my views.py

def profile_view(request, *args, **kwargs):
    context = {}
    user_id = kwargs.get("user_id")
    try:
        profile = Profile.objects.get(user=user_id)
    except:
        return HttpResponse("Something went wrong.")
    if profile:
        context['id'] = profile.id
        context['user'] = profile.user
        context['email'] = profile.email
        context['profile_picture'] = profile.profile_picture.url

        return render(request, "main/profile_visit.html", context)

urls.py

urlpatterns = [
    path("user/<user_id>/", views.profile_view, name = "get_profile"),
...

models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete = models.CASCADE, null = False, blank = True)
    first_name = models.CharField(max_length = 50, null = True, blank = True)
    last_name = models.CharField(max_length = 50, null = True, blank = True)
    phone = models.CharField(max_length = 50, null = True, blank = True)
    email = models.EmailField(max_length = 50, null = True, blank = True)
    bio = models.TextField(max_length = 300, null = True, blank = True)
    profile_picture = models.ImageField(default = 'default.png', upload_to = "img/%y", null = True, blank = True)
    banner_picture = models.ImageField(default = 'bg_image.png', upload_to = "img/%y", null = True, blank = True)

    def __str__(self):
        return f'{self.user.username} Profile'


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source