如何根据用户名而不是 Django 中的 (id, pk) 获取用户对象
How to get user object based on username and not the (id, pk) in Django
我在使用 URL 中的用户名查看其他配置文件时遇到困难,我可以看到带有用户 ID 的页面,但看不到带有用户名的页面,这是 url现在 http://127.0.0.1:8000/user/30/, but I want to have this http://127.0.0.1:8000/user/reedlyons/。我知道我可以用 get_object_or_404 做到这一点,但我想知道是否有其他方法可以解决这个问题。
这是我的 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'
def profile_view(request, username):
context = {}
try:
user = User.objects.get(username=username)
profile = user.profile
context['username'] = user.username
context['email'] = profile.email
context['bio'] = profile.bio
context['profile_picture'] = profile.profile_picture.url
except User.DoesNotExist:
return HttpResponse("Something went wrong.")
return render(request, "main/profile_visit.html", context)
urls.py
urlpatterns = [
path("user/<str:username>/", views.profile_view, name = "get_profile"),
]
确保每个 user
实例都附加了 profile
我在使用 URL 中的用户名查看其他配置文件时遇到困难,我可以看到带有用户 ID 的页面,但看不到带有用户名的页面,这是 url现在 http://127.0.0.1:8000/user/30/, but I want to have this http://127.0.0.1:8000/user/reedlyons/。我知道我可以用 get_object_or_404 做到这一点,但我想知道是否有其他方法可以解决这个问题。
这是我的 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'
def profile_view(request, username):
context = {}
try:
user = User.objects.get(username=username)
profile = user.profile
context['username'] = user.username
context['email'] = profile.email
context['bio'] = profile.bio
context['profile_picture'] = profile.profile_picture.url
except User.DoesNotExist:
return HttpResponse("Something went wrong.")
return render(request, "main/profile_visit.html", context)
urls.py
urlpatterns = [
path("user/<str:username>/", views.profile_view, name = "get_profile"),
]
确保每个 user
实例都附加了 profile