繁体   English   中英

如何链接到 django 中其他用户的个人资料?

[英]How to link to other user's profile in django?

我一直在尝试的是单击帖子的作者(创建帖子的用户)我希望它将我重定向到该用户的个人资料,例如在 Instagram 中,当您单击帖子顶部的用户时,它会重定向您到他们的个人资料。 每次我这样做而不是看到帖子的作者个人资料时,我都会看到登录的用户个人资料。 我认为views.py文件或base.html中有问题。

视图.py

    def profile(request, pk=None):
        if pk:
            user = get_object_or_404(User, pk=pk)
        else:
            user = request.user
        args = {'user': user}
        return render(request, 'profile.html', args)

    def home(request):
        created_posts = Create.objects.all().order_by("-added_date")
        return render(request, 'base.html', {"created_posts": created_posts})

    def create(request):
        if request.method == 'POST':
            created_date = timezone.now()
            header1 = request.POST['header']
            content1 = request.POST['content']
            user = request.user
            created_obj = Create.objects.create(added_date=created_date, title=header1, content=content1, user=user)
            created_obj.save()
            print('create created')
            return redirect('home')
        else:
            print('create not created')
            return render(request, 'create.html')

模型.py

    class Create(models.Model):
        added_date = models.DateTimeField()
        title = models.CharField(max_length=200)
        content = models.CharField(max_length=200)
        user = models.ForeignKey(User, related_name='user', on_delete=models.CASCADE, default=1)

    class Profile(models.Model):
        user = models.OneToOneField(User, on_delete=models.CASCADE)
        image = models.ImageField(default='default.jpg', upload_to='profile_pics')

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

网址.py

    urlpatterns = [
        path('', views.home, name='home'),
        path('profile', views.profile, name='profile'),
        path('profile/<int:pk>/', views.profile, name='profile_pk'),
        path('create', views.create, name='create'),
    ]

profile.html(显示用户配置文件)

    {% extends 'home.html' %}
    {% block body%}
    <div class="content-section">
      <div class="media">
        <img class="rounded-circle account-img" src="{{ user.profile.image.url }}">
        <div class="media-body">
          <h2 class="account-heading">{{ user.username }}</h2>
          <p class="text-secondary">{{ user.email }}</p>
        </div>
      </div>
    </div>
    {% endblock %}

base.html(使用用户名显示所有用户的帖子)

    {% extends 'home.html' %}
    {% block body %}
      <ul action="{% url 'create' %}" class="container-sm list-group" style="margin-top: 200px;">
        {% for created_post in created_posts %}
        <li class="list-group-item">{{ created_post.title }}
          <a href="{% url 'profile_pk' pk=user.pk %}">{{ created_post.user }}</a>
          <p>{{ created_post.content }}</p>
          <div class="float-right">
            <form action="delete_create/{{ created_post.id }}/" action="post">
              <button type="submit" class="btn btn-outline-danger btn-sm">Delete</button>
            </form>
          </div>
          <div class="float-right">
            <a href="{% url 'edit' created_post.id %}" class="btn btn-outline-warning btn-sm" style="margin-right: 5px;" role="button">Edit</a>
          </div>
        </li>     
       {% endfor %}            
     </ul>
     {% endblock %}

home.html(显示登录用户的导航栏)

    <body>
<nav class="navbar navbar-expand-md fixed-top navbar-dark" style="background-color: rgba(0, 0, 0, 0.712);">
    <div class="container">
      <a class="navbar-brand" href="/">
        <img src="static/style/images/logowebdev-png.png" alt="logo" style="width: 60px; height: auto;">
      </a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExample07" aria-controls="navbarsExample07" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarsExample07">
        <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
          <li class="nav-item">
            <a class="nav-link"  style="margin-left: 30px;" href="/">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link"  style="margin-left: 30px;" href="/profile">profile</a>
          </li>
          <li class="nav-item">
            <a class="nav-link"  style="margin-left: 30px;" href="#">Pricing</a>
          </li>
        </ul>
        <ul class="navbar-nav ml-auto">
          {% if user.is_authenticated %}
          <div class="float-right">
              <li class="nav-item active">
                  <a class="nav-link" href="#">New post</a>
              </li>
          </div>
          <div class="float-right">
            <li class="nav-item active">
              <a class="nav-link" href="">{{ user.username }}</a>
            </li>
          </div>
          <div class="float-right">
            <li class="nav-item">
              <a class="nav-link" href="/logout">Logout</a>
            </li>
          </div>
          {% else %}
          <div class="float-right">
            <li class="nav-item">
              <a class="nav-link" href="/login">Login</a>
            </li>
          </div>
          <div class="float-right">
            <li class="nav-item">
              <a class="nav-link" href="/register">Register</a>
            </li>
          </div>
          {% endif %}
        </ul>
      </div>
    </div>
  </nav>
  {% block body %}

  {% endblock %}
</body>

在你的base.html文件更改

<a href="{% url 'profile_pk' pk=user.pk %}">{{ created_post.user }}</a>

<a href="{% url 'profile_pk' pk=created_post.user.pk %}">{{ created_post.user }}</a>

因为,您必须将帖子所有者的 id 传递给您的视图。 当您仅使用用户时,django 会检测经过身份验证的用户 object。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM