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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:31:17+00:00 2026-06-15T22:31:17+00:00

Problem in short: How do I display a dynamic image on a template, when

  • 0

Problem in short:

How do I display a dynamic image on a template, when the function to create the image requires arguments, like an arbitrary long list of coordinates..?

for example <img src="{% url getComplexImg coords %}"/>, where coords is a list of arbitrary length consisting of either integers or tuples of integers, and getComplexImg is a view which returns an image as HttpResponse where mimetype is image/png, and where the image is generated with the list of coordinates.

EDIT: I have added a solution (inspired by the first answer) to the bottom.

The problem, long version

Following is a simplification of what I’m trying to do

urls.py:

urlpatterns = patterns('',
    url(r'^getSimpleImg$', views.getSimpleImg, name='getSimpleImg'),
    url(r'^getComplexImg$', views.getComplexImg, name='getComplexImg'),
    url(r'^showAllImgs$', views.showAllImgs, name='showAllImgs')
    )

views.py:

...
from django.template import Context, loader
...

def getSimpleImg(request):
    return HttpResponse(getImg.simple(), mimetype="image/png")

def getComplexImg(request, coords_1, coords_2):
    return HttpResponse(getImg.complex(coords_1,coords_2), mimetype="image/png")

def showAllImgs(request):
    coords_1, coords_2 = data_module.get_all_coordinates()
    context = Context({
        'coords_1':coords_1,
        'coords_2':coords_2})
    template = loader.get_template('showall.html')
    return HttpResponse(template.render(context))

‘data_module.get_all_coordinates()’ is just a method which returns two lists of arbitrary length (the lists could contain either integers or tuples of integers).

showall.html:

<html>
...
<img src="{% url getSimpleImg %}"/>
<img src="{% url getComplexImg coords_1 coords_2 %}"/>
...
</html>

It is very easy to make Django display the image returned from ‘getSimpleImg’, because it doesn’t need any list arguments. But I’m struggling to make Django display the complex image, because I down’t know how to pass on an arbitrary list as argument from the template.

The code above obviously doesn’t work because Django can’t lookup ‘{% url getComplexImg coords_1 coords_2 %}’ in the urls.py.

How should this kind of problem be solved?

Should I for example somehow send the images with the context? like:

views.py:

...
def showAllImgs(request):
coords_1, coords_2 = data_module.get_all_coordinates()
context = Context({
    'img_1':getImg.simple(),
    'img_2':getImg.complex(coords_1,coords_2)})
template = loader.get_template('showall.html')
return HttpResponse(template.render(context))

showall.html:

<html>
...
<img src="{{ img_1 }}"/>
<img src="{{ img_2 }}"/>
...
</html>

(above doesn’t work, it’s just to illustrate what I mean)

OR should I somehow import all the stuff I need into the template and then create and display images from there..? like:

showall.html:

{% import data_module %}
{% import getImg %}
<html>
...
<img src="{% getImg.simple() %}"/>
<img src="{% getImg.complex(data_module.get_all_coordinates()) %}"/>
...
</html>

Solution, inspired by first answer

I am solving it for now by passing on the context as:

Context({'coords_1':"_".join([str(v) for v in coords_1])})

and catching the url in urls.py with :

url(r'^getComplexImg/(?P<coords_1>[0-9_\-]+)/$', views.getComplexImg, name='getComplexImg')

And in my getComplexImg view, I convert the stringed lists back to real lists with:

coords_1 = [int(v) for v in coords_1.split('_')]

It works and I am satisfied for now, but I have a bad feeling that this might not be the most optimal solution

  • 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-15T22:31:18+00:00Added an answer on June 15, 2026 at 10:31 pm

    It looks as though your urls.py is malformed.

    You can pass variables via your urls. By using the correct syntax (the variables as regexs) you can refer to these variables when calling the url() or {% url %}, as you are trying to do.

    This simple example comes from here:
    http://www.djangobook.com/en/2.0/chapter08.html#using-default-view-arguments

    urls.py

    urlpatterns = patterns('',
        (r'^blog/$', views.page),
        (r'^blog/page(?P<num>\d+)/$', views.page),
    )
    

    views.py

    def page(request, num='1'):
        # Output the appropriate page of blog entries, according to num.
        # ...
    

    The bit that matters here is: (?P<num>\d+)

    You can then pass whatever is in the <here> to your view function, eg: def my_view(request, here): ...

    In your case you would replace the following:

    url(r'^getSimpleImg$', views.getSimpleImg, name='getSimpleImg'),
    ...
    

    To something like this:

    url(r'^getSimpleImg/(?P<coord1>\d+)/(?P<coord2>\d+)/$', views.getSimpleImg, name='getSimpleImg'),
    ...
    

    You many not want to use named variables in your urls, but should that be the case you cannot use {% url %} either and would need to find another solution.

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

Sidebar

Related Questions

Short problem: #include <iostream> using namespace std; int main() { double **T; long int
To make a long story short, I'm trying to pass a list of dictionaries
First - a short description of a problem. I write a JavaScript function to
I have a problem, I can't solve properly. In short: I want to create
So the wonderful low down on this doozie of a problem: short version: We
Problem in short: How could one implement static if functionality, proposed in c++11, in
Short problem description: XCode 4.2 install right Target on device, but debug (run) always
I'm currently playing with reflection and I have problem with my short code: public
SHORT DESCRIPTION OF PROBLEM: I want to set the text of a searchbar without
Short explanation of my problem, I need to parse an object and put its

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.