异常类型:DoesNotExist|配置文件匹配查询不存在



我试图在URL模式中获取用户名,而不是id<a class="dropdown-item" href="{% url 'UserApp:profile' username=profile.username%}">Profile</a>,因此在注销函数中出现错误:配置文件匹配查询不存在。但是配置文件模板正在呈现将所有查询都没有任何错误。如果我在URL中也使用id,一切都很好!请向我建议如何消除这个错误。非常感谢。

视图.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'),
]

我使用BaseUserManager、AbstractBaseUser进行用户注册和客户配置文件模型来获得用户名

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

终端错误

File "C:UsersSONJOYAppDataLocalProgramsPythonPython39libsite-packagesdjangocorehandlersbase.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UsersSONJOYAppDataLocalProgramsPythonPython39libsite-packagesdjangocontribauthdecorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "D:TrackingSystemUserAppviews.py", line 49, in profile
profile = Profile.objects.get(username=username)
File "C:UsersSONJOYAppDataLocalProgramsPythonPython39libsite-packagesdjangodbmodelsmanager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:UsersSONJOYAppDataLocalProgramsPythonPython39libsite-packagesdjangodbmodelsquery.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

如果访问/logouturl,它将与profile视图匹配,因为这是第一个匹配的url模式。您应该将其放在最后,以便首先匹配signuploginlogout等视图,并且只有当这些视图失败时,您才"火;profile视图,因此:

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 ↓
path('<str:username>/', views.profile, name='profile'),
]

最新更新