当前位置: 动力学知识库 > 问答 > 编程问答 >

python - For statement in django templates doesn't work

问题描述:

I have a problem with {% for %} loop in my django templates.

models.py:

-*- coding: utf-8 -*-

from django.db import models

class Country(models.Model):

title = models.CharField(max_length=100, verbose_name="Country")

published = models.DateTimeField(verbose_name="Date")

def __unicode__(self):

return self.title

class Nodes(models.Model):

node = models.CharField(max_length=150, verbose_name="Node")

panelists = models.IntegerField()

def __unicode__(self):

return self.node

views.py:

from django.shortcuts import render

from countries.models import Country

from countries.models import Nodes

def nodes(request):

return render(request, 'country/country.html', {'nodes' : Nodes.objects.all()})

def countries(request):

return render(request, 'countries/countries.html', {'countries' : Country.objects.all()})

def country(request, country_id):

return render(request, 'country/country.html', {'country' : Country.objects.get(id=country_id)})

And in my template country.html I have:

<h2 class="title">{{ country.title }}</h2>

<nav>

<ul>

{% for n in nodes %}

<li>{{ n.node }}</li>

{% endfor %}

</ul>

</nav>

And that's doesn't work. Could You help me please?

I know if I change country.html file like this:

<h2 class="title">{{ country.title }}</h2>

<nav>

<ul>

{% for n in nodes %}

<h2>TEST</h2>

<li>{{ n.node }}</li>

{% endfor %}

</ul>

</nav>

I can't see "TEST" too. So all this for statement is overlooked.

网友答案:

If you are trying to go example.com/country/country_id you won't be able to print the nodes, since they aren't in the context dic in your view function. Try doing it like this:

def country(request, country_id):
    context_dict = {}
    try:
        nodes = Nodes.objects.all()
        context_dict['username'] = nodes

        country =  Country.objects.filter(id=country_id)
        context_dict['posts'] = country

    except Country.DoesNotExist:
        return redirect('index')
    return render(request, 'country/country.html', context_dict, )

One of the mistakes I think you have made is Country.objects.get(id=country_id) since you are just getting the ID and I could see in your template you are trying to get the Country title. The best thing to do is, since you are trying to get the page for a specific country_id, you have to use the filter when you are trying to query the Country model. And don't forget about the urls.py. It should look something like this

url(r'^country/(?P<country_id>\d+)/$', views.country, name='country'),

Give it a try and let me know, if it still doesn't work and what kind of errors are you getting.

网友答案:

OK, I solved the problem. I didn't know I can't make two function relate to the same template. Now views.py looks:

    def country(request, country_id):
        return render(request, 'country/country.html', {'country' : Country.objects.get(id=country_id), 'nodes' : Nodes.objects.all()})

分享给朋友:
您可能感兴趣的文章:
随机阅读: