2014-12-04 33 views
0

我的模板代码:不能在Django模板比较varriables

  <select name="channelChoice" id="channelChoice" class="choiceBox"> 
      <option value="0">Chose your channel</option> 
      {% if channels %} 
       {% for ch in channels %} 
        {% if ch.id == default_channel_id %} 
         <option class="channel-choice" value="{{ ch.id }}" selected="selected" >{{ ch.title }}</option> 
        {% else %} 
         <option class="channel-choice" value="{{ ch.id }}" >{{ ch.title }}</option> 
        {% endif %} 
       {% endfor %} 
      {% endif %} 
     </select> 

我的Python代码:

def index(request): 
channels = models.Channel.objects.filter(activated=True).order_by('title') 
default_channel_id = request.GET.get('channel_id', False) 
if not default_channel_id: 
    default_channel_id = 1 
return render(request, 'epgadmin/index.html', {'channels': channels, 'default_channle_id': default_channel_id}) 

我通过default_channel_id与值是1,和渠道的清单包含的频道使用完全相同的ID是1.但是如果从句没有达到!谁能帮我?

+0

正确default_channel_id的拼写在这一行:“返回渲染(请求'epgadmin/index.html',{'channels':channels,'default_channle_id':default_channel_id})“。 – Ymartin 2014-12-04 05:21:05

回答

0

你不会在查询需要models

from models import Channel 
channels = Channel.objects.filter(activated=True).order_by('title') 
+0

你能解释一下吗? – 2014-12-04 03:54:47

+0

我不知道你为什么要用'model'。你没有'输入'它检查这个https://docs.djangoproject.com/en/dev/topics/db/queries/ – shellbye 2014-12-04 03:59:35

+0

你可以在视图中打印你的'Channel'和'default_channel_id'来看看它是什么你要。 – shellbye 2014-12-04 04:00:52

0

你必须在变量一个错字,你传递给模板:default_channle_id

+0

错字是什么意思?你的意思是打字吗? – 2014-12-04 04:14:19

+0

你需要'default_channel_id',但你传递了'default_channle_id'。这是一个拼写错误,意味着拼写错误。 – shellbye 2014-12-04 05:10:55