我得到了上述错误,我已经调整了我的代码的基础上,我看到的答案在线,但仍然得到相同的错误
意见. PY '
def profile(request, user_id):
profile_user = User.objects.get(pk = user_id)
# user_profile = Profile.objects.get(user=user_object)
profile_post = NewTweet.objects.filter(user = user_id)
post_count = len(profile_post)
follower = request.user.id
user = user_id
if Followers.objects.filter(follower=follower, user=user).first():
button_text = "Unfollow"
else:
button_text = "Follow"
context = {
"profile_user": profile_user,
"post_count": post_count,
"profile_post": profile_post,
"button_text": button_text,
}
return render(request, "network/profile.html", context)
'
URL.PY
path("profile/<int:user_id>", views.profile, name="profile"),
布局. HTML '
<li class="nav-item">
<a class="nav-link" href="{% url 'profile' user.id %}">
<img src= {% static 'network/images/profile.svg' %} alt="Profile" class="left-sidebar-menu-icon">
Profile
</a>
<img src= {% static 'network/images/profile.svg' %} alt="Profile" class="left-sidebar-menu-icon">
Profile
</li>
'
MODELS.PY
class Profile(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
id_user = models.IntegerField()
bio = models.TextField(blank=True)
def __str__(self):
return self.user.username
class Followers(models.Model):
follower = models.CharField(max_length=100)
user = models.CharField(max_length=100)
# follower = models.ForeignKey('User', on_delete=models.CASCADE, related_name='targets')
# target = models.ForeignKey('User', on_delete=models.CASCADE, related_name='followers')
def __str__(self):
return self.user
In Layout.html, I try '{{ user.id }}' to see what is reflecting and it shows 'None'. Please advise
In Layout.html, I try '{{ user.id }}' to see what is reflecting and it shows 'None'.
我还调整了这一建议在不同的线程,但相同的错误消息
<a class="nav-link" href="{% url 'profile' user_id=user.id %}">
我希望它会显示一个按钮文本"Follow"或"Unfollow",具体取决于用户当前是否在关注当前用户
请指示
1条答案
按热度按时间kqlmhetl1#
在视图中创建的上下文中没有
user
:user.id
为None,因为没有名为user
的变量。由于视图中已经有user_id
,因此只需将其添加到上下文中,并在模板中使用user_id
而不是user.id
。