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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:42:08+00:00 2026-05-11T20:42:08+00:00

I’m a PHP guy on my first day in Python-land, trying to convert a

  • 0

I’m a PHP guy on my first day in Python-land, trying to convert a php site to python (learning experience), and I’m hurting for advice. I never thought it would be so hard to use multi-dimensional arrays or dictionaries as you pythoners call them.

So I can create multi-dimensional arrays using this, but i can’t loop it in a django template. this doesnt work but i imagine i cant loop through it if i could get it to work.

{% for key,val in dictionary.items %}

only works for actual dictionaries it seems, not the custon multi-dimensional dictionary classes.

I’m creating my dictionary from a sql query:

vid[ video[ 7 ] ][ 'cat_short_name' ] = video[ 2 ]
vid[ video[ 7 ] ][ 'cat_name' ] = video[ 1 ]
vid[ video[ 7 ] ][ 'cat_id' ] = video[ 7 ]

vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'comp_short_name' ] = video[ 5 ]
vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'comp_name' ] = video[ 4 ]
vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'comp_website' ] = video[ 6 ]

vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'videos' ][ video[ 8 ] ][ 'top_video' ] = 0
vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'videos' ][ video[ 8 ] ][ 'vid_id' ] = video[ 8 ]
vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'videos' ][ video[ 8 ] ][ 'vid_name' ] = video[ 9 ]
vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'videos' ][ video[ 8 ] ][ 'vid_url' ] = video[ 10 ]

I basically need to get all companies in a certain category and then get all videos in that company so i can nest them easily in my template. This is how i did it in php, creating one huge deep array. Trying to duplicate in Python has proven difficult.

I thought maybe i could do with the backwards lookups in django using set_MODEL but i couldn’t figure that out either.

Any help on how to accomplish my goal would be appreciated. I hope my question is clear

EDIT:

When im done looping in my template it looks like this…

<h1>Category</h1>
   <h2>Company</h2>
   <ul>
        <li>video</li>
    </ul>
    <h2>Company</h2>
    <ul>
        <li>video</li>
        <li>video</li>
    </ul>
<h1>Category</h1>
   <h2>Company</h2>
   <ul>
        <li>video</li>
    </ul>
    <h2>Company</h2>
    <ul>
        <li>video</li>
        <li>video</li>
    </ul>
  • 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-11T20:42:08+00:00Added an answer on May 11, 2026 at 8:42 pm

    You should be using the built in ORM instead of using your own queries (at least for something simple like this), makes things much easier (assuming you’ve also built your models in your models.py file)

    In your view:

    def categories_view(request):
        categories = Categories.objects.all()   #maybe put an order_by or filter here
        return render_to_response("your_template.html", {'categories':categories})
    

    In your template:

    {% for category in categories %}
        <h1>{{ category.name }}</h1>
        {% for company in category.company_set.all %}
            <h2>{{ company.name }}</h2>
            <ul>
            {% for video in company.video_set.all %}
                <li>{{ video.name }}</li>
            {% endfor %}
            </ul>
        {% endfor %}
    {% endfor %}
    

    I haven’t tested it but it should work. Compare this code to what you would have to write if you weren’t using ORM, in either PHP or Python.

    take a look at the django docs for more info, I’d recommend taking a few hours and doing the tutorial.

    Update: modified the code to use “_set.all”

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This question / answer might be of help. Edit -… May 11, 2026 at 11:52 pm
  • Editorial Team
    Editorial Team added an answer "edge of a knurled volume control" sounds like the QwtWheel… May 11, 2026 at 11:52 pm
  • Editorial Team
    Editorial Team added an answer i've got a metaclass that reads this information and does… May 11, 2026 at 11:52 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.