如何摆脱“%20个人资料”在 URL 模式中
首先,我将搜索用户,然后将我带到用户列表的页面并单击。之后,当我选择要单击以查看其个人资料的用户时,它应该将我带到我想查看的用户页面,但这就是我遇到的问题。 URL 模式应该是,例如 /user/MatthewRond/,但我得到的是 /user/MatthewRond%20Profile/,这使我无法看到 Matthew 的页面,因为 % 20Profile 从未打算出现在 URL 中。如果我能弄清楚如何摆脱 %20Profile,我的问题应该得到解决。
我阅读了 %20 是什么以及我从中得到的信息:它与使用和处理字母数字字符有关。不管怎样,这些都没有帮助我弄清楚如何删除它。至于它的代码部分,我从数据库中获取默认用户名,然后获取从该默认用户创建的自定义配置文件。之后,我将用户名设置为我制作的自定义配置文件模型。
视图.py
def profile_view(request, username, *args, **kwargs,):
context = {}
try:
user = User.objects.get(username=username)
profile = user.profile
img = profile.uploads_set.all().order_by("-id")
context['username'] = user.username
except:
return HttpResponse("Something went wrong.")
if profile and img:
context = {"profile": profile, "img": img}
return render(request, "main/profile_visit.html", context)
搜索结果.py
<a class="profile-link" href="{% url 'profile_view' username=profile.0 %}">
网址.py
path("user/<str:username>/", views.profile_view, name = "profile_view"),
模型.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'
First, I will search up the user, and then it takes me to a page of a list of users to click onto. After that, when I choose a user I want to click on to check out their profile, it should take me to users page I want to see, but that is where I have a problem. The URL pattern should be, for example /user/MatthewRond/, but instead, I get /user/MatthewRond%20Profile/, which prevents me from seeing Matthew's page because %20Profile was never intended to be in the URL. If I can figure out how to get rid of the %20Profile, my problem should be solved.
I read up on what %20 was and what I got from it: it has to do with using and dealing with alphanumeric characters. Regardless none of it helped me figure out how to remove it. As for the code part of it, I am grabbing the default username from the database then grabbing the custom profile I made from that default user. After that, I set the username to the custom profile model I made.
views.py
def profile_view(request, username, *args, **kwargs,):
context = {}
try:
user = User.objects.get(username=username)
profile = user.profile
img = profile.uploads_set.all().order_by("-id")
context['username'] = user.username
except:
return HttpResponse("Something went wrong.")
if profile and img:
context = {"profile": profile, "img": img}
return render(request, "main/profile_visit.html", context)
search_results.py
<a class="profile-link" href="{% url 'profile_view' username=profile.0 %}">
urls.py
path("user/<str:username>/", views.profile_view, name = "profile_view"),
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Rapheal 的帮助下,错误将出现在 Profile 类中。把这个改成
这个。
删除空格和配置文件
With the help of Rapheal, the error would be in the Profile class. Change this
to this.
Remove the space and the Profile