Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6969125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:32:29+00:00 2026-05-27T16:32:29+00:00

I am using Django to generate a template formatted as a plist to be

  • 0

I am using Django to generate a template formatted as a plist to be directly used within an iPhone app. However, I am having trouble getting the plist to output correctly based on my database. Essentially, I have a table containing tests. Each with a testID and a test_type. The table uses the unique_together clause to make sure no two entries have the same testID and test_type and it also orders results by testID and test_type. But in my template, I’d like to group all of the tests with the same testID within the same dictionary.
My template looks something like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>all_tests</key>
    <array>
        {% for t in tests %}
        {% ifchanged t.testID %}<dict>
            <key>testID</key>
            <string>{{ t.testID }}</string>
            <key>sections</key>
            <array>{% endifchanged %}
                <dict>
                    <key>pk</key>
                    <integer>{{ t.pk }}</integer>
                    <key>type</key>
                    <string>{{ t.test_type }}</string>
                    <key>num_questions</key>
                    <integer>{{ t.num_questions }}</integer>
                </dict>
            {% ifchanged t.testID %}</array>
        </dict>{% endifchanged %}{% endfor %}
    </array>

Essentially, I wanted the {% if changed %} directive to evaluate based on the the last time it evaluated, not within the same loop. But of course this isn’t the actual behavior, because it naturally checks its values based on the last loop iteration. How should I produce my desired output? Also, the tests array is generated via:

tests = Test.objects.annotate(num_questions=Count('questions')).filter(num_questions__gt=0).all()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:32:30+00:00Added an answer on May 27, 2026 at 4:32 pm

    The {% ifchanged %} should work if the “tests” set is ordered by testID.

    Have you tried:

    tests = Test.objects\
                   .annotate(num_questions=Count('questions'))\
                   .filter(num_questions__gt=0)\
                   .order_by('testID').all()
    

    You can also take a look at {% regroup %} tag.

    [updated]

    How about a couple tests for forloop.{first,last}? (sorry, untested…)

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>all_tests</key>
        <array>
            {% for t in tests %}
            {% ifchanged t.testID %}
              {% if not forloop.first %}
                </array>
              </dict>
              {% endif %}
              <dict>
                <key>testID</key>
                <string>{{ t.testID }}</string>
                <key>sections</key>
                <array>{% endifchanged %}
                    <dict>
                        <key>pk</key>
                        <integer>{{ t.pk }}</integer>
                        <key>type</key>
                        <string>{{ t.test_type }}</string>
                        <key>num_questions</key>
                        <integer>{{ t.num_questions }}</integer>
                    </dict>
              {% if forloop.last %}
                </array>
              </dict>
              {% endif %}
        </array>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using http://code.google.com/p/django-email-extras/ to generate rich email content. I have trouble in sending email
I dynamically generate my html using the django form template everything works fine except
Context: HTML widgets generated using a Django ModelForm and template, jQuery 1.3.2, JavaScript on
I want to generate a list using my django model say I have these
I'm using Django to write a blog app, and I'm trying to implement a
I am having problems using django-tagging . I try to follow the documentation but
I try to smuggle HTML template in the HTML for mustache.js, however the django
Does Django have any template tags to generate common HTML markup? For example, I
I'm using Pisa/XHTML2PDF to generate PDFs on the fly in Django. Unfortunately, I need
I'm using pyBarcode to generate PNGs and the number below the barcode is getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.