tags:

views:

66

answers:

3

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 %}
+3  A: 

That's incorrect syntax. Try this:

{% if GET.search % }

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

Peter Rowell
+1 rule of thumb, you never use dictionary syntax in a template.
Chase Seibert
@Chase: Unfortunately, I am unconvinced of the existence of these people called "template designers." That means that the template is either being done by a designer ... nah, ain't gonna happen, or a programmer ... yeah, that's what happens in my neighborhood. I think they really need to look at reality here and go with Jinja2 or something very close to it (it's faster, too).
Peter Rowell
+5  A: 

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 %}
Jarret Hardie
It's not true to say you can't use filter syntax within a tag. That completely depends on how the tag is written - you can definitely evaluate filters within the tag, if the tag calls the relevant resolving functions.
Daniel Roseman
+1  A: 

First define blocks, then add code inside

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