Django: How to display author of query of posts?
<p>I'm trying to make individual pages for each author showing their name and posts. I can't seem to get the username displayed.</p>

<p>views.py</p>

<pre><code>class UserProfileView(generic.ListView):
    template_name = 'howl/user-profile.html'
    context_object_name = 'user_howls'

    def get_queryset(self):
        author = self.request.user
        u = User.objects.get(username=author)
        return Howl.objects.filter(author=u)
</code></pre>

<p>models.py</p>

<pre><code>class Howl(models.Model):
    author = models.ForeignKey(User, null=True)
    content = models.CharField(max_length=150)
</code></pre>

<p>Here is where I'm stuck.</p>

<p>user-profile.html</p>

<pre><code>{% extends 'howl/base.html' %}

{% block content %}

&lt;h1&gt;User: {{user_howl.author}}&lt;/h1&gt;

{% for user_howl in user_howls %}
&lt;ul&gt;
    &lt;li&gt;{{user_howl.content}}&lt;/li&gt;
&lt;/ul&gt;
{% endfor %}

{% endblock %}
</code></pre>

<p>The content is displayed just fine, but the heading just says "User: ", how do I give it a context without using a for loop?
I've tried:</p>

<pre><code>{% for author in user_howls.author %}
&lt;h1&gt;User: {{author}}&lt;/h1&gt;
{% endfor %}
</code></pre>

<p>and </p>

<pre><code>{% if user_howls.author %}
&lt;h1&gt;User: {{user_howl.author}}&lt;/h1&gt;
{% endif %}
</code></pre>

<p>Still the same outcome, displaying "User: "</p>

<p><code>user_howls</code> is a queryset so it won't have an <code>author</code> attribute, you need to get the author of the iterated object</p>

<pre><code>{% for howl in user_howls %}
&lt;h1&gt;User: {{ howl.author}}&lt;/h1&gt;
{% endfor %}
</code></pre>

<p>More to the point though, it doesn't make sense to start from a <code>Howl</code> list, when you are just returning the results for the user_profile, nor does it make sense to use a <code>ListView</code>. so instead, start from the user and then look up its howls</p>

<pre><code>user_obj.howl_set.all()
</code></pre>

python django