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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:15:24+00:00 2026-06-10T06:15:24+00:00

Every time I code something using Django, I am faced to the same issue

  • 0

Every time I code something using Django, I am faced to the same issue (I think this is due to my lack of experience using this framework):

I know how to pass arguments I had from a queryset to a template and how to display it but I don’t know how to add argument computed in the view and use them in the template.

Simple View (I master it) :
Example: Retrieve a list of Pizzas and display them in a template

views.py

Pizzas = Pizza.objects.all()
return render_to_response( "pizza.html" , {'pizzas':Pizzas} )

pizza.html

{% for pizza in pizzas %}
    <li>pizza.name</li>
{% endfor %}

But, let’s say I want to add some arguments linked to the queryset but which are not in the database like something I computed in the view, I don’t know how to pass this argument to the template and how to use it -> EDIT : I want to order all my pizza by total calories in the template.

Example : For each Pizzas, I have computed the number of calories

views.py

Pizzas = Pizza.objects.all()
tab = []
for pizza in Pizzas:
    # Compute some data and return the total number of calories for one pizza
    total_number_calories = XXX
    tab.append({'p':pizza,'calories':total_number_calories'})        
return render_to_response( "pizza.html" , {'pizzas_calories':tab} )

pizza.html

?

I am not even sure my way to pass these additional data to the template is good (creating a table and pass it as an argument to the template.

If you have any idea or best practices to do that in Django I’ll take it (and lot of people using Django will do the same !)

  • 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-06-10T06:15:25+00:00Added an answer on June 10, 2026 at 6:15 am

    You should always try and do as much as possible in the view. With the code you’ve shown, ordering by total calories is just one extra line after the for loop:

    tab.sort(key=lambda t: t['calories'])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every time I get something from the db, my code is this: using (SqlConnection
This demo code loops 3 times and every time appends text. What I need
Every time I need to do something N times inside an algorithm using C#
I have an input type='text', and using jQuery, every time this textbox is blurred
I made some code to produce boxes every time my code encounters a white
In my IF statement, its kinda weird, cause every time my code loops in
In the following code every time button is clicked the alter shows the value
Every time I run the code below it is supposed to come up with
The JavaScript code below is executed every time the user selects the login button
Thinking about avoiding code replication, I got a question that catches me every time

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.