1👍
Change your MEDIA_URL
like this:
MEDIA_URL = '/media/'
Then change upload_to
parameter like this:
photo = models.ImageField(upload_to='images')
Add this to your urls.py
:
from django.conf import settings
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
)
Then access the uploaded image in your template like below:
<img src="{{ picture.photo.url }}">
0👍
Start by changing MEDIA_URL –
MEDIA_URL = '/media/'
Then change the upload_to parameter on your model
photo = models.ImageField(upload_to='images')
Add an images folder to your MEDIA_ROOT directory (I can’t tell where that is as I don’t know what your DIRNAME
variable is)
/path/to/your/media_root/images
Now you can display a photo in a template using –
{% your_object.photo.url %}
Don’t hard code url’s in your template –
<img class="preview_image" src="static/images/test_1.png" ale="photo" />
should be –
<img class="preview_image" src="{% your_object.photo.url %}" ale="photo" />
Have a look at the docs on ImageField and serving static files.
When you save your model (with a photo), the photo is uploaded to <MEDIA_ROOT>/<upload_to>
. It’s then served up over http from http://localhost:8000/<MEDIA_URL>/<upload_to>
. However since you’re saving your photo in a model, the database keeps a reference to the location of the file allowing you to load the image with <img src="{% object.photo.url %}">
.
- [Answer]-Django Migrate – change charfield to datetimefield
- [Answer]-How to moderate django_openid_auth
- [Answer]-Django, get an object from a list of filtered objects which has the maximum value for a field