Django return objects to HTML from view with queryset?
<p>I trying to show some info in HTML from query set in Django. But I don't know the best way to do it.</p>

<p>The idea is when I open the page this show me the name of user and the competitions related with it. The user can create competition in admin of Django.</p>

<p>My url is that.</p>

<pre><code>url(r'^home/(?P&lt;company_name&gt;\w+)', HomeView.as_view(), name='home'),
</code></pre>

<p>The model.</p>

<pre><code>class Competition(models.Model):
    name = models.CharField(max_length=200,null=False)
    image = models.CharField(max_length=200, null=False)
    url = models.CharField(max_length=200, null=False)
    startingDate = models.DateTimeField(null=False)
    deadline = models.DateTimeField(null=False)
    description = models.CharField(max_length=200, null=False)
    user = models.ForeignKey(User, null=False)
</code></pre>

<p>In views.py</p>

<pre><code>class HomeView(ListView):
    model = User, Competition
    template_name = 'home.html'
    context_object_name = 'company'

    def get_queryset(self, **kwargs):
        company = self.kwargs['company_name']
        try:
            queryset = User.objects.filter(username__exact=company).get()
        except User.DoesNotExist:
            queryset = None
        return queryset
</code></pre>

<p>The code above return me the User, when I write in url something like this: <a href="http://127.0.0.1:8000/home/diego/" rel="nofollow">http://127.0.0.1:8000/home/diego/</a></p>

<p>The HTML home show me the name of User for example:</p>

<pre><code>{% if company %} &lt;div class="jumbotron"&gt; &lt;div class="container"&gt;
    {{company.username}}  &lt;/div&gt;&lt;/div&gt; {% endif %}
</code></pre>

<p>But, I need to show the competitions created by the User with a 'for' in HTML. </p>

<p>I tried with chains return the two query set but I don't work it.</p>

<p>Also this.</p>

<pre><code>queryset2 = Competition.objects.filter(user__username__exact=company).get()
</code></pre>

<p>The get() show me the error that return more one object. That is logic because the user has 3 competitions related with this.</p>

<p>Some little example for that? or any idea for resolve my question.</p>

<p>Thanks a lot </p>

<p>I Tried this, but the 'User' object is not iterable (Obviously it is just one)</p>

<pre><code>class HomeView(ListView):
model = User
template_name = 'home.html'
context_object_name = 'company'

def get_queryset(self, **kwargs):
    company = self.kwargs['company_name']

    try:
        queryset = User.objects.filter(username__exact=company).get()
        queryset2 = Competition.objects.filter(user__username__exact=company)
        result_list = list(chain(queryset, queryset2))
    except User.DoesNotExist:
        result_list = None
    return result_list
</code></pre>

<p>I have been trying and trying and delete the get() for first query set and the data return to HTML.</p>

<p>How can I access to the fields of the list in HTML but with the class, If I print the variable 'company' the result is that.</p>

<pre><code>[&lt;User: diego&gt;, &lt;Competition: test1&gt;, &lt;Competition: test2&gt;]
</code></pre>

<p>But with 'for' just I obtain the string values. How can I difference the 'User' and 'Competition'</p>

<pre><code> {%for compa in company%} 
   {{compa}} 
 {%endfor%}
</code></pre>

<p>the result is:
diego
test1
test2</p>

<p>you dont have to return the different querysets from get_queryset, Its complicated. List view has one method called get_context_data"
you can override it to fill the extra objects or data you want.</p>

<p>for example just write</p>

<pre><code>class HomeView(ListView):
    model = User, Competition
    template_name = 'home.html'
    context_object_name = 'company'

    def get_queryset(self, **kwargs):
        company = self.kwargs['company_name']
        try:
            queryset =    User.objects.filter(username__exact=company).get()
        except User.DoesNotExist:
            queryset = None
            return queryset
    def get_context_data(self, **kwargs):
        context =  super(HomeView, self).get_context_data(**kwargs)
        queryset2 = Competition.objects.filter(user__username__exact=company)
        context['competitions'] = queryset2
        return context
</code></pre>

<p>Here you will be getting company and "competitions" separately. Use them well in templates. just try it. It should work </p>

python html django django-queryset