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?