0

I am trying to get the username in the URL pattern instead of id <a class="dropdown-item" href="{% url 'UserApp:profile' username=profile.username%}">Profile</a> and so getting an error in the logout function: Profile matching query does not exist. But the profile template is rendering will all the query without any error. If I use id in the URL as well everything is working fine! Please suggest to me how can I get rid of the error. Thank you!

Views.py

def logout_view(request):
    logout(request)
    return redirect("index")


@login_required
def profile(request, username):
    title = 'Profile'
    context={'title': title}
    profile = Profile.objects.get(username=username)
    if profile:
        context.update(profile = profile)
    else:
        return HttpResponse("user is not found")
    return render(request, 'UserApp/profile.html', context)

Urls.py

from django.urls import path
from UserApp import views

app_name = 'UserApp'

urlpatterns = [
    path('<str:username>/', views.profile, name='profile'),
    path('signup/', views.signup, name='signup'),
    path('login/', views.login_view, name='login'),
    path('logout/', views.logout_view, name='logout'),
    path('update/', views.update_profile, name='update'),
]

I used BaseUserManager, AbstractBaseUser for user registration and customer profile models to get the username

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
    username = models.CharField(max_length=264, null=True, blank=True)
    full_name = models.CharField(max_length=264, blank=True)
    address_1 = models.TextField(max_length=300, blank=True)
    city = models.CharField(max_length=40, blank=True)
    zipcode = models.CharField(max_length=10, blank=True)
    country = models.CharField(max_length=50, blank=True)
    date_joined = models.DateTimeField(auto_now_add=True)
    pro_pic = models.ImageField(upload_to=None, blank=True)
    about = models.CharField(max_length=500, blank=True)

    def __str__(self):
        return self.username

    def is_fully_filled(self):
        fields_names = [f.name for f in self._meta.get_fields()]

        for field_name in fields_names:
            value = getattr(self, field_name)
            if value is None or value == '':
                return False
        return True

Error in the terminal

  File "C:\Users\SONJOY\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\SONJOY\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "D:\TrackingSystem\UserApp\views.py", line 49, in profile
    profile = Profile.objects.get(username=username)
  File "C:\Users\SONJOY\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\SONJOY\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 429, in get
    raise self.model.DoesNotExist(
UserApp.models.Profile.DoesNotExist: Profile matching query does not exist.
[15/Apr/2021 09:52:47] "GET /user/logout/ HTTP/1.1" 500 77227

1 Answers1

0

If you visit the /logout url, it will match the profile view, since that is the first url pattern that matches. You should put that last, such that the signup, login, logout, etc. view are first matched, and only if these fail, you "fire" the profile view, so:

from django.urls import path
from UserApp import views

app_name = 'UserApp'

urlpatterns = [
    path('signup/', views.signup, name='signup'),
    path('login/', views.login_view, name='login'),
    path('logout/', views.logout_view, name='logout'),
    path('update/', views.update_profile, name='update'),    
    # put this last &downarrow;
    path('<str:username>/', views.profile, name='profile'),
]
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Better yet add a prefix or suffix to the url pattern: `user//`. This makes your urls more informative, prevents such situations, generally better. – Abdul Aziz Barkat Apr 18 '21 at 17:03
  • Thank you for your help and sorry for the delay. Your solution is working fine and I did not face any issue yet.. following this trick. – Sonjoy Pall Apr 20 '21 at 14:00