I'm building a gallery using Django(1.5.1) on my local machine. In my Album model I have a ImageField
. There is a view to show all images of an album. It works well, but at the end images don't show up. There's border of images as you can see but images don't load.
class Category(models.Model):
###
class Album(models.Model):
category = models.ForeignKey(Category, related_name='albums')
###
class Image(models.Model):
album = models.ForeignKey(Album)
image = models.ImageField(upload_to = 'images/albums/')
def detail(request, album_id):
album = get_object_or_404(Album, pk=album_id)
return render(request, 'gallery/detail.html', {'album': album})
<h1>{{ album.title }}</h1>
{% for image in album.image_set.all %}
<a> <img src="{{ image.image.url }}" height="420"></a>
{% endfor %}
If this is my album address: http://localhost:8000/gallery/1/
Then image URL is:http://localhost:8000/media/images/albums/photo_4.JPG (I get 404 when enter it in browser)
This media root and url:
MEDIA_ROOT = '/media/'
MEDIA_URL = '/localhost:8000/media/'
My media root has 777 permission.
What should I do now? Where is the problem?
you can pass your base_image to get_image. html page using dictionary content = {"base_image": base_image} and use {{base_image}} key at src attribute in get_img. html template to display.
In Django, a default database is automatically created for you. All you have to do is add the tables called models. The upload_to tells Django to store the photo in a directory called pics under the media directory. The list_display list tells Django admin to display its contents in the admin dashboard.
Template inheritance. The most powerful – and thus the most complex – part of Django's template engine is template inheritance. Template inheritance allows you to build a base “skeleton” template that contains all the common elements of your site and defines blocks that child templates can override.
{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.
I have a clue on what's the problem. MEDIA_URL
should be like this:
MEDIA_ROOT='<the full path to your media folder>' (i.e: '/home/ike/project/media/')
MEDIA_URL='/media/'
Note the slash character at the beginning. That is because media is a folder in your root server folder and not relative to whatever other url you call it.
And add these lines to the end of your urls.py
file:
# You might need to import static function like this:
#from django.contrib.staticfiles.urls import static
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
You can check the following documentation: https://docs.djangoproject.com/en/dev/howto/static-files
Hope this helps
If you're using the dev server then you need to add something to your urls.py to make django serve the media files, cf:
1.4.x : https://docs.djangoproject.com/en/1.4/howto/static-files/#serving-other-directories 1.5.x: https://docs.djangoproject.com/en/dev/howto/static-files/#serving-files-uploaded-by-a-user
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With