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 %}">
添加回答
舉報