gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:08:52 24 4
gpt4 key购买 nike

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

View .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 传递给您的 View 。当您仅使用用户时,django 会检测经过身份验证的用户对象。

关于python - 如何在 django 中链接到其他用户的个人资料?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61434535/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com