显示用户在 django 中上传的图像

发布于 2024-11-05 05:23:33 字数 2085 浏览 0 评论 0原文

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

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

提前非常感谢

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'^

Thanks a lot in advance

, 'gharnivas.views.index'), url(r'^search/

Thanks a lot in advance

, 'gharnivas.views.search'), url(r'^venture/(?P<ghar_id>\d+)/

Thanks a lot in advance

, 'gharnivas.views.venture'), url(r'^media/(?P<path>.*)

Thanks a lot in advance

,'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}), url(r'^admin/', include(admin.site.urls)), )

Thanks a lot in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

司马昭之心 2024-11-12 05:23:33

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

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"/>
谁许谁一生繁华 2024-11-12 05:23:33

在您的情况下:

冒险图片在这里

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

In your case:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文