1

Before someone marks this as duplicate I have read and tried solutions in these threads:

Use Django Template Tags in img src

django 1.5 - How to use variables inside static tag

Django: Insert image in a template whose path is dynamic

And still have not been able to get it to work. So I'm using Django built in UpdateView to update an data base entry and I'm trying to load image to template where part of src is dynamic like this:

edit_journal_entry_form.html

{% extends 'base.html' %}
{% load staticfiles%}
<form method="post">
    {% csrf_token %}
    {% static "" as baseUrl %}
    <img src="{{ baseUrl }}/lpr_images/{{journalEntry.license_plate_nr_img}}"></img>
    <img src="{% static "" %}/lpr_images/{{journalEntry.license_plate_nr_img}}" />
    <img id="edit_img" src="{% static 'lpr_images/' %}{{journalEntry.license_plate_nr_img}}" alt="Image not read!"/>
    {{ form.as_p }}
    <button class="btn btn-success" type="submit">Submit</button>
...

views.py

class JournalEntryUpdate(UpdateView):
    model = JournalEntry
    template_name = 'gate_operator/edit_journal_entry_form.html'
    success_url = '/gate_operator/journal/'
    fields = [
        'license_plate_nr',
        'license_plate_nr_img',
        ...
    ]

    def form_valid(self, form):
        object = form.save(commit=False)
        object.user = self.request.user
        object.save()
        return super(JournalEntryUpdate, self).form_valid(form)

models.py

class JournalEntry(models.Model):
    license_plate_nr = models.CharField(max_length=20, blank=True)
    license_plate_nr_img = models.CharField(max_length=100, blank=True)
    ...

None of this works in console I can se that I'm getting only the static part: GET http://127.0.0.1:8000/static//lpr_images/ 404 (Not Found) I tried to hard code the url just to make sure I'm on the right path, so this successfully shows image: http://127.0.0.1:8000/static//lpr_images/2018_04_26_08_43_25.png So what am I missing here or doing wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
furang
  • 93
  • 1
  • 12
  • If `{{journalEntry.license_plate_nr_img}}` isn't outputting `2018_04_26_08_43_25.png` as expected, then you need to fix that. But you haven't shown your view or model so we can't help with that. – Alasdair Apr 26 '18 at 21:42
  • @Alasdair I added views.py and models.py. – furang Apr 26 '18 at 22:09
  • @Alasdair And it apears You were right about checking `{{journalEntry.license_plate_nr_img}}` it does not output anything, so the problem probably is there not in dynamic link. Thanks! – furang Apr 26 '18 at 22:17

1 Answers1

1

In the template, you should use journalentry (all lowercase), instead of journalEntry.

Alasdair
  • 298,606
  • 55
  • 578
  • 516