开发者

Django bug with a tag

My code

{% if GET['search'] % }
        开发者_开发百科    {% block count codes|length as counter %}

Is the following a valid if -clause as a Django tag?

{% if GET['search'] % }

The following code gives me an error that block takes only one argument. What is wrong in the code?

{% block count codes|length as counter %}


Django isn't PHP.

You're trying to use a template filter inside a template tag. You can use either a tag or a filter, but not both.

For that matter, since the block tag takes only a label for the block, I'm not sure what the template code you've written is supposed to do. Additionally, are you sure that GET['search'] is valid syntax in a template tag?

I'm guessing a little at your view and template requirements, but here's how I would approach this in your place. There are a number of gaps you'll have to fill in for yourself depending on your circumstances.

In views.py:

from django.shortcuts import render_to_response

def my_view(request):
    request_was_search = False
    codes = []

    if request.GET.has_key('search'):
        request_was_search = True
        codes = some_function_you_define_to_get_codes()

    return render_to_response('foo.html',
                              {'codes':codes,
                               'request_was_search':request_was_search})

In the template:

{% block count %}
    {% if request_was_search %}
        // do whatever you want here
        <p>There were {{ codes|length }} codes submitted.</p>
        // end example
    {% endif %}
{% endblock %}


That's incorrect syntax. Try this:

{% if GET.search % }

This assumes you have GET in the context passed to your template.


First define blocks, then add code inside

{% block header %}
 <h1>My Site</h1>
{% endblock header %}


Look at the first line of your code: {% if GET['search'] % } The extra space between the last two characters % and } is the problem. Remove the space so that %} are close together. The error ('block' tag takes only one argument) would go away.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜