【问题标题】:one user viewing another users profile in django一个用户在 django 中查看另一个用户的个人资料
【发布时间】:2020-11-19 14:13:38
【问题描述】:

我想实现一个功能,让另一个用户 A 点击另一个用户 B 的图片并自动重定向到用户 B 的个人资料。我该怎么做?请查看我的 HTML,我在其中说明了有关链接的内容

view.py

class profile(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE)
bio = models.TextField(blank=True)


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

html:

{% for post in posts %}
  <div class="icl-NavigationList-item">
   <div class="icl-NavigationList-link icl-NavigationList--primary">
   <div class="icl-NavigationList-text">
   <h2 class="icl-NavigationList-title">
     <div class="upperText">
      <h2 class="card-title"style="background-color:{{post.post_colours}};">{{post.job_title}}</h2>
    <a class="a-tags" href="*{{ i need to link the post author's profile  here}}*" data-tippy-content="@dribble_handle">
  <img  src="{{post.author.profile.profile_pic.url}}" style="border-radius: 100%;float:right;" 
  alt="author"width="30" height="30"></a></div>
  <div class="lowerText"> <p class="card-text">{{post.currency}} {{post.salary}}</p>
   <p class="card-text"> Posted on {{post.date_posted}} by {{post.author}} </p>
  <br>
   {% if user.is_authenticated %}

我的模特

class profile(models.Model):
   user = models.OneToOneField(User, on_delete = models.CASCADE)
   bio = models.TextField(blank=True)
  category = models.CharField(max_length= 1000, choices = 
  Select_category,default='other')



def get(self, request, *args, **kwargs):
    username = self.kwargs['username']
    user_profile = profile.objects.get(user__username=username)
    gigs = Gig.objects.filter(user__username=username, status=True)
    print(user_profile.values())
    return render(request, 'blog/profile.html', {'user_profile': user_profile, 'gigs': gigs,'name': username})

【问题讨论】:

  • 你的问题是......?到目前为止,您已经给出了代码并说出了您想要的,但您还没有解释该代码的输出有什么问题
  • 我该怎么做?
  • 去看看什么?就像我编辑了我的评论一样——你有一些关于你想要什么的声明和一堆代码。如果您只是询问如何实现某些东西,那么代码将是无用的分心,我会投票关闭它,因为需要更多细节。 碰巧,您的代码看起来很合理,但我仍然不知道您遇到了什么问题
  • 我想知道如何让一个用户查看另一个用户的个人资料先生.. 我不知道如何做到这一点
  • 没有。绝对不。你可以用edit 来解释你的问题。我不是个人帮助台

标签: python html django model


【解决方案1】:

好的,这是经过编辑的答案。我看了你的代码,你这里没有帖子模型,所以这只是我试图把一些片段放在一起尝试。最终,我们的目标应该是让 href 看起来像这样:

href="{{ post.author.get_absolute_url }}"

注意这里不是 {{ post.author.profile.get_absolute_url }}。在 url 中省略 profile 似乎违反直觉,但请记住,传递的用户名参数是 User 模型的一部分,而不是 profile 模型。

(注意:url 肯定取决于您的帖子的模型。如果作者是用户模型的 ForeignKey,请使用我在此处输入的内容。如果是与配置文件模型,您可以将作者替换为 author.user)

如果这不起作用,请确保您的 urls.py 设置正确。

urls.py

...
path(‘URL_YOU_WANT/<username>/, views.VIEW_NAME, name=“NAME”),
...

(注意:保持“”完全一样。这就是 Django 知道期望您的 href 传递的用户名参数的方式。)

如果仍然无法正常工作,我会重写您的 views.py 视图以使其更清晰。您的views.py 和models.py 有一些冗余和混乱。以下是我的做法:

models.py

class profile(models.Model):
     user = models.OneToOneField(User, on_delete = models.CASCADE)
     bio = models.TextField(blank = True)

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

views.py

def user_profile(request, username):
     user = User.objects.get(username = username)
     user_profile = profile.objects.get(user = user)
     gigs = Gig.objects.filter(user = user, status = True)
     return render(request, ‘blog/profile.html’, {‘user_profile’: user_profile, ‘gigs’: gigs, ‘name’: username})

如果这有帮助,请告诉我!

【讨论】:

  • 是的,我试过了..但我仍然无法路由到另一个用户的个人资料..我认为我仍然做错了什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-16
  • 1970-01-01
  • 2023-03-22
  • 2021-09-04
  • 2020-12-14
  • 1970-01-01
相关资源
最近更新 更多