2 回答

TA贡献1784条经验 获得超2个赞
首先,您需要通过覆盖get_context_data(...)方法来传递一个上下文变量,它决定用户Items是否有
class UserPostListView(ListView):
# rest of your code
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
has_items = Item.objects.filter(designer__username=self.kwargs['username']).exists()
context['has_items'] = has_items
return context
然后在你的模板中,使用这个has_items变量,
{% if has_items %}
<a class="primary btn-lg" href="{% url 'core:designer-posts' username=view.kwargs.username %}" role="button">Go to items</a>
{% endif %}
另外,您没有将 传递username给url标签,应该是
{% url 'core:designer-posts' username=view.kwargs.username %}
在这里,我曾经从 URL 中view.kwargs.username获取用户名

TA贡献1859条经验 获得超6个赞
在 {% url %} item.designer 中传递一个用户对象,你必须像这样提供 id
<a href="{% url 'core:designer-posts' item.designer.id %}">
添加回答
举报