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 7696681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:50:12+00:00 2026-05-31T21:50:12+00:00

I have a function in view.py which returns two variables i.e. a dictionary named

  • 0

I have a function in view.py which returns two variables i.e. a dictionary named ca and a list named categories:

def eventcateg_detail(request):
    ca = EventTypeCategory.objects.values()
    categories =[]
    for i in range(0, len(ca)):
       pk_sub = ca[i]['sub_categ_id']
       if (pk_sub!=None):
          category = EventTypeCategory.objects.get(id = pk_sub)
          category = category.name
          categories.append(category)
    return render(request,"events/categ.html",{
      'obj': ca,
      'categ' :categories
      })

categ returns:

[u'Critical in list', u'Information in list', u'Amit Pal in list']

My categ.html is:

<thead>
        <tr><th>{% trans "Name Category" %}</th><th>{% trans " Message" %}</th><th>{% trans "Sub-categories" %}</th></tr>
    </thead>
    <tbody>
    {% for obj in obj %}
        <tr>

            <td>{{ obj.name }}</td><td> {{ obj.Message_slug }}</td><td></td>

        </tr>
    {% endfor %}
    </tbody>
</table>

My output should be:

Event Category  Event Message   Sub-categories
Critical            critical    critical in list
Information         information information in list
Amit Pal            amit-pal    amit pal in list

Sub categories contain the data coming from the list. I am getting Event Category (using obj.name) and Event Message (using obj.Message_slug) right but I’m not able to find any way to put sub-categories also in the same td and the same for loop (look at the template).

  • 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-31T21:50:13+00:00Added an answer on May 31, 2026 at 9:50 pm

    I would suggest that you look at this: Django, category and subcategories

    This will probably require some changes to your model but will simplify your code a lot.

    UPDATE

    I will answer your first question then 🙂

    You can nest for loops so in your case:

    <thead>
        <tr><th>{% trans "Name Category" %}</th><th>{% trans " Message" %}</th><th>{% trans "Sub-categories" %}</th></tr>
    </thead>
    <tbody>
    {% for obj in obj %}
        <tr>
    
            <td>{{ obj.name }}</td><td> {{ obj.Message_slug }}</td>
            <td>
                {% for i in categ %}
                      {{ i }}
                {% endfor %}
            </td>
    
        </tr>
    {% endfor %}
    </tbody>
    

    However! The above code points to a weakness in your eventcateg_detail method. The list that you create does not tell which parent-category the list item belongs to so for each parent object all items will be printed.

    If you know that there will always be a one-to-one relationship and there will always be one child only you can create a dictionary instead

    Also, I believe that your initial code can be simplified:

    def eventcateg_detail(request):
            ca = EventTypeCategory.objects.all()
            categories ={}
            for cat in ca:
                if cat.sub_categ_id:
    
                   #Since the ca queryset already contains all items you can use that to get the child category
                   category = ca.objects.get(id=cat.sub_categ_id)
    
                   #Add to dictionary. NOTE: If you ever get more than one sub-category 
                   #you need to build a list of sub-categories before adding it to the dict.
                   categories[cat.id] = category.name
            return render(request,"events/categ.html",{ 'obj': ca, 'categ' :categories})
    

    In your template you do this:

    <thead>
    <tr>
         <th>{% trans "Name Category" %}</th>
         <th>{% trans " Message" %}</th><th>{% trans "Sub-categories" %}</th>
    </tr>
    </thead>
    <tbody>
    {% for o in obj %}
    <tr>
        <td>{{ o.name }}</td><td> {{ o.Message_slug }}</td>
        <td>
            {% for key, value in categ.items %}
                  {% ifequal key o.id %}
                   {{ value }}
                  {% endifequal %}
            {% endfor %}
        </td>
    
    </tr>
    

    {% endfor %}

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view function which needs to be manually transaction managed, but when
I try to have some inheritance with attributeBindings between two View. (function(exports) { Ember.MobileBaseView
I have a class called User with static function loginRequired(), which returns false if
I have a view. the view has a function that returns a text string
i have an init() function that returns a list of Domaines needed to populate
I have the view function in django that written like a dispatcher calling other
I have a function in a view that renders the xml in the browser,
In the main view i have this function public function lang($file, $language){ require 'languages/'.$language.'/'.
I have coded a view that relies on a scalar function value for one
I have a view (actually, it's a table valued function, but the observed behavior

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.