[Answer]-Associate Existing Image File with Django Model

1👍

Well you have to save it somewhere in order to map the user and the image….

So I can think of 2 solutions –

  1. Have a field that stores the path. Then you can access it via the template, something like this – <img src="{{author.photo_path}}">

  2. If you’re saying the path might change, you can have a user ID for each user, and store his photo with that ID. Then you can simply do <img src="/path/{{author.id}}_picture.gif">

Using the second solution, you can also have the path as a variable in your code, and then pass it to the template, and access the picture like this –
<img src="/{{path}}/{{author.id}}_picture.gif">, which doesn’t force you to have a hardcoded path, and once the path changes you only change it once, not in all your model instances/template files

👤thomas

Leave a comment