我是 Django 新手,我正在尝试显示每个用户的个人资料。到目前为止,我已经制作了一个登录、注册系统。用户也可以更新他们的个人资料,但一旦登录用户点击其他用户的用户名,我真的无法显示每个用户的个人资料。我想知道是否可以对每个配置文件的主键/ID 做一些事情,并使用它来访问每个配置文件。一点帮助将不胜感激!
这是我的代码的样子:
个人资料页面的 URL:
path('profile/', views.userprofile, name='userprofile'),
我查看个人资料的观点:
@login_required
def userprofile(request):
Post = post.objects.order_by('-created')
return render(request,'social_media/profile.html', {'Post':Post,'Profile':Profile})
用户资料的型号:
class profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
desc = models.CharField(max_length=1000)
pfp = models.FileField(default='images/profile.jpg', upload_to='profile_pics/', blank=True)
cover = models.FileField(default='images/profile.jpg', upload_to='cover_pics/', blank=True)
将我带到个人资料 URL 的链接:
<div class="user-pfp-name">
<a href="{% url 'userprofile' %}">
<img src="{% static 'social_media/images/profile.jpg' %}" style="height: 35px; width: auto; border-radius: 100%;" alt="">
{{ i.user.first_name }}
{{ i.user.last_name }}
</a>
</div>
最后是我的个人资料页面:
<div class="profile">
<div class="coverpad">
{% if Profile.cover %}
<img src="{{ Profile.cover.url }}">
{% endif %}
</div>
<div class="profilepic">
{% if Profile.pfp %}
<img src="{{ Profile.pfp.url }}" height="140px">
{% endif %}
</div>
<div class="profile-middle">
<div class="profile-username">{{ user.first_name }} {{ user.last_name }}</div>
{% if Profile.desc %}
<div class="profile-desc"></div>
{% endif %}
</div>
</div>