0

[Not really sure if the topic makes sense, but didn't found a more meaningful one.]

I've created a template which looks like:

{% for x in jobs %}
        <table>
            <tr>
                <td></td>
                <td>{{ x.Ecordov.oovorder }}</td>
            </tr>
            <tr>
                <td></td>
                <td>{{ x.ooaname1.split('{}')[0] }}</td>
            </tr>
            <tr>
                <td></td>
                <td>{{ x.ooaname2.split('{}')[0] }}</td>
            </tr>
            <tr>
                <td></td>
                <td>{{ x.ooazusatz.split('{}')[0] }}</td>
            </tr>
        </table>
{% endfor %}

As you can see, I'm getting a specific position in multiple lists, which works quite well.

The problem I'm trying to solve: These lists have up to 16 positions, which I have to render. I could of course copy/paste the above <tr> </tr> block 16 times into the template, and edit the line positions, but I'm quite sure that there is a better, more automated, way; however, I wasn't able to find this out on my own up until now.

Could anyone point me into the correct direction?

Thanks for any help and all the best!

2
  • You could use getattr(x, attr) to grab the value within the attribute. Armed with this, you could do something like for attr in ['ooaname1', 'ooaname2', ...], and simply do the rendering with {{ getattr(x, attr).split(....) }}. Ideally you want that list of attributes be stored in your code rather than the template for organization. Commented Jul 3, 2015 at 10:05
  • Hi, thanks for your comment. In the end I've used the method described by doru. Regarding your last sentence: I fully agree, however, I had problems to inject both the context ("jobs") and the list of attributes into the template. Commented Jul 3, 2015 at 14:51

1 Answer 1

1

Try this:

{% for x in jobs %}
    {% for i in range(0, 17) %}
        <table>
            <tr>
                <td></td>
                <td>{{ x.Ecordov.oovorder }}</td>
            </tr>
            <tr>
                <td></td>
                <td>{{ x.ooaname1.split('{}')[i] }}</td>
            </tr>
            <tr>
                <td></td>
                <td>{{ x.ooaname2.split('{}')[i] }}</td>
            </tr>
            <tr>
                <td></td>
                <td>{{ x.ooazusatz.split('{}')[i] }}</td>
            </tr>
        </table>
    {% endfor %}
{% endfor %}

If you don't know how many elements does the list have you have to find it first and use it as the stop argument (the second argument of the range() function).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.