【问题标题】:show images that are uploaded by user in django显示用户在 django 中上传的图像
【发布时间】:2011-05-05 08:28:01
【问题描述】:

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

我的html代码sn-p是<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)),
)

提前非常感谢

【问题讨论】:

    标签: django django-templates django-media


    【解决方案1】:

    您可以创建一个单独的对象,该对象可以使用 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"/>
    

    【讨论】:

      【解决方案2】:

      &lt;img src="{{ [MODEL_OBJECT].[IMAGE_FIELD].url }}" /&gt;

      在你的情况下:

      &lt;img border="2" src="{{ghar.image.url}}" alt="venture picture here" /&gt;

      【讨论】:

      • 您在模型中使用upload_to 吗?或者以某种方式手动处理上传?如果是第二个 - src="{{ MEDIA_URL }}/{{ghar.image.name}}"(或 src="{{ MEDIA_URL }}/images/{{ghar.image.name}}"
      • @DrTyrsa:我已经更新了这个问题。你能检查一下,请告诉我我的错误吗?
      • 如我所见,ghar 对象没有image 属性,对吧?所以你不能表现出来。 ghar 可以有多个图像,您应该决定显示哪个。如果您想要第一个,那么src="{{ ghar.images.0.image.url }}" 将起作用,但最好将这个代码属性设为GharData 模型。
      • @DrTyrsa:我试过了,但还是没有结果。问题是图像存储在文件夹/project/templates/media/image/ 中。背景的正常图像被加载,因为我在 view.py 中渲染了背景图像。现在它没有显示对象ghar(s) 图像。关于要为 GharData 创建的属性,我应该只创建一个新文件吗?那应该是什么文件?
      • ghar.images.0.image.url 输出什么? ghar.images 查询集中有任何对象吗?我认为您需要为您的模型GharData 创建一个属性image 并为self.images 查询集返回所需的图像。
      猜你喜欢
      • 2016-05-04
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      相关资源
      最近更新 更多