简体   繁体   中英

show images that are uploaded by user in django

I have a page which allows a user to upload an image and that image is stored by the models.py file in the {{MEDIA_ROOT}}/image/image1.jpg i want to display this image through a html image tag on another web page

My html code snippet is <img border="2" src="{{ MEDIA_URL }}/images/{{ghar.image}}" alt="venture picture here"/>

here ghar is the object that has the details abt the image

Can anyone help me to do the same

i am using Django as the framework and sqlite3 as the DB

Update :

This is my models.py function

from django.db import models

class GharData(models.Model):
    place = models.CharField(max_length=40)
    typeOfProperty = models.CharField(max_length=30)
    typeOfPlace = models.CharField(max_length=20)
    price = models.IntegerField()
    ownerName = models.CharField(max_length=80)
    ownerEmail = models.EmailField(blank=True, null=True)
    address = models.CharField(max_length=200)
    nameOfVenture = models.CharField(max_length=100)
    city = models.CharField(max_length=60)
    size = models.CharField(max_length=60)
    designedBy = models.CharField(max_length=50,blank=True,null=True)
    description = models.CharField(max_length=500,blank=True,null=True)
    ownerPhone = models.CharField(max_length=20)


    def __unicode__(self):
        return self.ownerName


class ImageData(models.Model):
    property = models.ForeignKey(GharData, related_name='images')
    image = models.ImageField(upload_to='image/',blank=True,null=True)
    video = models.FileField(upload_to='video/',blank=True,null=True)

    def __unicode__(self):
        return self.property.ownerName

This is the urls.py

from django.conf.urls.defaults import patterns, include, url
import settings

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()



urlpatterns = patterns('',
    url(r'^$', 'gharnivas.views.index'),
        url(r'^search/$', 'gharnivas.views.search'),
    url(r'^venture/(?P<ghar_id>\d+)/$', 'gharnivas.views.venture'),
    url(r'^media/(?P<path>.*)$','django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),

   url(r'^admin/', include(admin.site.urls)),
)

Thanks a lot in advance

<img src="{{ [MODEL_OBJECT].[IMAGE_FIELD].url }}" />

In your case:

<img border="2" src="{{ghar.image.url}}" alt="venture picture here" />

You can crate a separate object which has the access to ImageData class with the id of GharData class.

def Show_something(request, ghar_id):
    g = get_object_or_404(GharData, pk=ghar_id)
    i = g.images.all()
    return render_to_response('YOUR_APP/iventure.html', {'ghar': g, 'imagedata' :i }, context_instance=RequestContext(request))

<img  border="2" src="{{ MEDIA_URL }}{{ imagedata.image }}" alt="venture picture here"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM