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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:03:23+00:00 2026-05-27T15:03:23+00:00

I am fairly new to Django and I am having trouble getting values to

  • 0

I am fairly new to Django and I am having trouble getting values to load into the HTML from the dictionary generated in the models.py that looks like this:

>>> generic_id = Generic.objects.get(pk=127)
>>> dict = generic_id._get_dict()
>>> dict  
[{'field__name':'key1', 'value_char':'value1a', 'value_num':'value1'},{'field__name':'key2', 'value_char':'value2a', 'value_num':'value2'},{'field__name':'key3', 'value_char':'value3a', 'value_num':'value3'},{'field__name':'key4', 'value_char':'value4a', 'value_num':'value4'}]
>>> dict[2]['value_num']  
Decimal('value2')
>>> dict[3]['value_char']
'value3a'

The HTML table looks like this:

<table>
  <tr>
    <td>Description1</td><td>{{value1}}</td>
    <td>Description2</td><td>{{value2}}</td>
    <td>Description3</td><td>{{value3a}}</td>
    <td>Description4</td><td>{{value4}}</td>
  </tr>
  <tr>
    <td>Name: {{ generic.name }}</td>
    <td>E-mail: {{ generic.email }}</td>
    <td>State: {{ generic.state }}
</table>

The code in the views.py right now looks like this:

def query_test(request, generic_id):
    try:
        a = Generic_table.objects.get(pk=generic_id)
    except Generic_table.DoesNotExist:
        raise Http404
    t = loader.get_template('query_test.html')
    c = RequestContext(request, {
             'generic' : a, })
    return HttpResponse(t.render(c))

Can someone give me some suggestions as to how to (and efficiently) get the appropriate values from the dictionary into the generated HTML?

  • 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-27T15:03:24+00:00Added an answer on May 27, 2026 at 3:03 pm

    Based on what your objects look like from your model, and your template, I would suggest trying this:

    assuming:

    a = Generic.objects.get(pk=generic_id)
    # a == {'field__name':'key1', 'value_char':'value1a', 'value_num':'value1'}
    

    views.py

    from django.shortcuts import render_to_response, get_object_or_404
    
    def query_test(request, generic_id):
        a = get_object_or_404(Generic, pk=generic_id)
    
        return render_to_response("query_test.html", a, 
                        context_instance=RequestContext(request))
    

    query_test.html

    Name: {{field__name}}
    Char: {{value_char}}
    Num : {{value_num}}
    

    Your view doesn’t show that you are expecting more than one object, since you look for an id, so your template would end up formatting just one object.

    Edit: In case you are trying to display a list of results

    views.py might look something like this:

    def query_test(request, generic_id=None):
        if generic_id is not None:
            a = Generic.objects.filter(pk=generic_id)
        else:
            a = Generic.objects.all()
    
        c = {'generic': a}
    
        # add some extra values
        c['value1'] = "I am value1"
    
        # add a whole dictionary of other values
        c.update({'value2': "yay val2", 'value3a': 3, 'value4': "I am 4"})
    
        return render_to_response("query_test.html", c, 
                        context_instance=RequestContext(request)) 
    

    And your template something like:

    <table>
    <tr>
        <td>Description1</td><td>{{value1}}</td>
        <td>Description2</td><td>{{value2}}</td>
        <td>Description3</td><td>{{value3a}}</td>
        <td>Description4</td><td>{{value4}}</td>
    </tr>
    {% for item in generic %}
        <tr>
            <td>Name: {{item.field__name}}</td>
            <td>Char: {{item.value_char}}</td>
            <td>Num : {{item.value_num}}</td>
        </tr>
    {% endfor %}
    </table>
    

    Edit2: Addressing the strange object you are sending to your template

    Based on your updated question… That is not a dictionary. Its a list of dictionaries, and Its really strange the way you are pulling that data from the single model instance. But assuming that is what you really really want, you have a number of options..

    1) Fix that data object BEFORE sending it to the template. I have no idea if you want all the elements in that list, or just a specific item.

    not_a_generic_id = Generic.objects.get(pk=127)
    not_a_dict = not_a_generic_id._get_dict()
    dict_that_I_actually_want = not_a_dict[1]
    return render_to_response("query_test.html", dict_that_I_actually_want)
    

    2) Send that entire list to the template and loop over each item, and then access the values:

    views.py

    not_a_generic_id = Generic.objects.get(pk=127)
    not_a_dict = not_a_generic_id._get_dict()
    c = {"still_not_a_dict": not_a_dict}
    return render_to_response("query_test.html", c)
    

    template.html

    <table>
    {% for actual_dict in still_not_a_dict %}
        <tr>
            <td>Name: {{actual_dict.field__name}}</td>
            <td>Char: {{actual_dict.value_char}}</td>
            <td>Num : {{actual_dict.value_num}}</td>
        </tr>
    {% endfor %}
    </table>
    

    3) Even though the template does not let you actually access numeric indexes of lists because you are suppost to sort that data out yourself in the view…if you insist on accessing a specific index of that list in the template, do the same as #2 for the views.py, but:

    template.html

    {% for actual_dict in still_not_a_dict %}
        {% if forloop.counter == 1 %}
            {{actual_dict.value_char}}
        {% endif %}
    {% endfor %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to Django and having read the documentation on its relational models
Still fairly new to django and python. I've defined two nearly identical models that
Being fairly new to programming, I am having trouble understanding exactly what Homebrew does...
Fairly new to database schema (plan to use SQLite). Having said that, I'm thinking
Im still fairly new to Django, so please explain things with that in mind.
Fairly new to .Net here and having trouble with a line of code. I
Fairly new to Spring, so I'm having some trouble with this. I'm trying to
I'm fairly new to Django having up to this point used to it display
I am fairly new to Django and i am trying to use the builtin
i'm fairly new to django and just trying a couple simple experiments to get

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.