如何摆脱“%20个人资料”在 URL 模式中

发布于 2025-01-10 06:19:00 字数 1977 浏览 0 评论 0原文

首先,我将搜索用户,然后将我带到用户列表的页面并单击。之后,当我选择要单击以查看其个人资料的用户时,它应该将我带到我想查看的用户页面,但这就是我遇到的问题。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双马尾 2025-01-17 06:19:00

在 Rapheal 的帮助下,错误将出现在 Profile 类中。把这个改成

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

这个。

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

删除空格和配置文件

With the help of Rapheal, the error would be in the Profile class. Change this

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

to this.

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

Remove the space and the Profile

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文