开发者

Why doesnt the If statement in django work?

开发者 https://www.devze.com 2023-03-11 11:11 出处:网络
I was working from an example of an \'if\' statement, but for some reason I cannot get it to work. I tried every possible statement in the \'if\', but it seems as though nothing makes it true for the

I was working from an example of an 'if' statement, but for some reason I cannot get it to work. I tried every possible statement in the 'if', but it seems as though nothing makes it true for the if statement to run. Instead the Else statement runs.

Sample

{% extends 'base.html' %}
{% block content %}

<h2>Hardware Inventory</h2>

{% if hardware %}

<table id="datatable">
    <thead>
        <tr>
            <th>Name</th>
            <th>Manufacturer</th>
            <th>Category</th>
        </tr>
    </thead>
    <tbody>
    {% for item in hardware %}
        <tr>
            <开发者_运维技巧td><a href="/inventory/hardware/{{ item.id }}">{{ item.name }}</a></td>
            <td>{{ item.manufacturer }}</td>
            <td>{{ item.kind }}</td>
        </tr>
    {% endfor %}
    </tbody>
</table>

{% else %}

<p>The inventory is empty.</p>

{% endif %}


{% endblock %}

Mine

{% extends 'base.html' %}
{% block content %}

<h2>News</h2>

{% if entry %}

{% for item in entry %}
<table id = "news">
    <tr>
        <td><a href="/news/Entry/{{ item.id }}">{{ item.title }}</a></td>
        <td>{{ item.body }}</td>
        <td>{{ item.pub_date }}</td>
    </tr>
</table>
{% endfor %}



{% else %}
<p>No News</p>
{% endif %}


{% endblock %}

My view.py for news, Im not sure how to write it correctly, but i tried different combinations, this one errors at the moment causes it to crash

def index(request):
    return render_to_response('news/index.html', {'Entry': Entry}, context_instance=RequestContext(request))

def Entry(request):
    Entry = Entry.objects.all().order_by('pub_date')
    return render_to_response('news/Entry.html', {'item':item}, context_instance=RequestContext(request))


Make sure you're actually passing entry into the context. For example:

render_to_response('template.html', { 'entry': entry })

Unset variables behave as variables set to None in Django templates.

UPDATE:

Made some revisions to your view code; not even sure how you got to the template rendering with what you had.

Original:

def index(request):
    return render_to_response('news/index.html', {'Entry': Entry}, context_instance=RequestContext(request))

def Entry(request):
    Entry = Entry.objects.all().order_by('pub_date')
    return render_to_response('news/Entry.html', {'item':item}, context_instance=RequestContext(request))

Modified:

def index(request):
    entry = Entry.objects.all().order_by('pub_date')
    return render_to_response('news/index.html', {'entry': entry}, context_instance=RequestContext(request))

I don't think you even needed the Entry method, so removed that. I kept your naming the same, but it's better form to call that variable entries since it's multiple items.

0

精彩评论

暂无评论...
验证码 换一张
取 消