1

我有一个允许用户上传图像的页面,该图像由 {{MEDIA_ROOT}}/image/image1.jpg 中的 models.py 文件存储我想通过另一个网站上的 html 图像标签显示此图像页

我的 html 代码片段是<img border="2" src="{{ MEDIA_URL }}/images/{{ghar.image}}" alt="venture picture here"/>

这里 ghar 是具有图像细节的对象

谁能帮我做同样的事情

我使用 Django 作为框架,使用 sqlite3 作为数据库

更新

这是我的 models.py 函数

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

这是 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)),
)

非常感谢提前

4

2 回答 2

0

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

在你的情况下:

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

于 2011-05-05T08:36:30.997 回答
0

您可以创建一个单独的对象,该对象可以使用 GharData 类的 id 访问 ImageData 类。

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"/>
于 2011-05-05T10:37:21.297 回答