1

I have a report.html template in which I need to dynamically change the name of images. I've done a lot of research and trial and error. But I just can't get the URLs for the images to be correct.

The images are in /templates/users/reports/rptemplate/images.

After researching static images, I also copied the images to:

/static/images.

Here's my latest html:

<?xml version="1.0" encoding="UTF-8"?>

{% load staticfiles %} 

<html>
...

              <img alt="" src="static/images/{{img_vision}}">
...

This is my report view:

class UserReportView(LoginRequiredMixin, TemplateView):
    model = User
    template_name = 'users/reports/rptemplate/report.html'
    def get_context_data(self, **kwargs):
        #context = super(DisplayTaskView, self).get_context_data(kwargs)
            #TODO: retrieve the actual data
        context = {'sid': 519893,
        'church_name': 'Lakeview Bible',
        'report_date': '5 Feb 2018',
        'responses': 57,
        'img_vision': 'image1.png',
        'img_leadership': 'image1.png',
        'img_mobilization': 'image1.png',
        'img_stewardship': 'image1.png',
        'img_context': 'image1.png',
        'img_evangelism': 'image1.png',
        'img_discipleship': 'image1.png',
        'img_service': 'image1.png',
        'img_fellowship': 'image1.png',
        'img_worship': 'image1.png',
        'img_category': 'image1.png',
        'img_radar': 'image1.png'
        }  
        return context

And this is my user/url.py:

from django.conf.urls import url
from django.views.generic import TemplateView
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

from . import views

urlpatterns = [
    url(
        regex=r'^$',
        view=views.UserListView.as_view(),
        name='list'
    ),
    url(
        regex=r'^~redirect/$',
        view=views.UserRedirectView.as_view(),
        name='redirect'
    ),
    url(
        regex=r'^(?P<username>[\w.@+-]+)/$',
        view=views.UserDetailView.as_view(),
        name='detail'
    ),
    url(
        regex=r'^~update/$',
        view=views.UserUpdateView.as_view(),
        name='update'
    ),
    url(
        regex=r'^reports/rptemplate/$',
        view=views.UserReportView.as_view(),
        name='report'
    ),
]

urlpatterns += staticfiles_urlpatterns()

So where is my mistake? How do I dynamically specify the name of a static file?

5
  • What exactly are you trying to do dynamically: get all the images within a certain directory, or to build a url to the image within a forloop? Commented Feb 7, 2018 at 17:47
  • I'm retrieving data from limesurvey and generating graphs. There are 10 graphs per report. And of course, multiple users can request the graph at the same time. So the images need to be generated with different files names for each user + graph combination. Commented Feb 7, 2018 at 17:51
  • Have you tried <img alt="" src="/static/images/{{img_vision}}"> note the extra slash before static Commented Feb 7, 2018 at 18:00
  • @Anshul Dang! That was it. If you make that an answer, I'll accept it. Commented Feb 7, 2018 at 18:07
  • Glad I could help. :) Commented Feb 7, 2018 at 18:09

1 Answer 1

2

Try <img alt="" src="/static/images/{{img_vision}}"> *Note the extra slash before static.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.