简体   繁体   English

在html模板中显示由管理员上传的图像

[英]show images uploaded by admin in html template

I am new to django and I saw solutions for my problem but none worked. 我是django的新手,但我看到了解决问题的方法,但没有一个有效。

I make admin upload images as you see in Item class in models.py : 如您在models.py的Item类中看到的,我让管理员上传图像:

from django.db import models
from django.contrib import admin


class Customer (models.Model):
    username = models.CharField(max_length=500)
    email = models.CharField(max_length=1000 , unique=True)
    phone = models.IntegerField()
    address = models.CharField(max_length=3000)


class Category(models.Model):
    category_title = models.CharField(max_length=100 , unique=True)

    def __str__(self):
        return self.category_title


class Order (models.Model):
    time = models.DateTimeField(auto_now_add=True)
    total = models.IntegerField()
    created_by = models.ForeignKey(Customer, related_name='orders')


class Item (models.Model):
    name = models.CharField(max_length=100 ,unique=True)
    details = models.CharField(max_length=1000)
    price = models.IntegerField()
    item_logo = models.ImageField(upload_to='res/static/res/images')
    category = models.ForeignKey(Category, related_name="items" ,on_delete= models.CASCADE)

    def __str__(self):
        return self.name


class ItemInline(admin.TabularInline):
    model = Item

class CategoryAdmin(admin.ModelAdmin):
    inlines = [
        ItemInline,
    ]

so how can I make those images appear in my HTML ( note: html code works fine but a small default image field appear instead of image ) : 因此,如何使这些图像显示在HTML中(请注意:html代码可以正常工作,但是会出现一个小的默认图像字段而不是image):

{% extends 'res/menu.html' %}
{% block content %}
<h1>hello</h1>

    <table class="table table-striped table-bordered" style="text-align: left ; border-color: black;" id="pizza_table">
        <tbody>
            {% for item in items %}
                <td>
                    {{ item.name }}
                    <p>{{ item.details }}</p>
                </td>
                <td>
                    <img src="{{item.item_logo}}">
                </td>
                <td>

                </td>
                <td>
                    <button type="button" class="btn btn-success" style="margin: 5px ;" onclick="additem('Seafood Pizza', 20 ) ;">Add</button>
                </td>
            {% endfor %}
        </tbody>
    </table>

{% endblock %}

thanks in advance 提前致谢

Qoute from the doc : 来自文档的 Qoute:

Using a FileField or an ImageField (see below) in a model takes a few steps: 在模型中使用FileField或ImageField(请参见下文)需要执行以下步骤:

In your settings file, you'll need to define MEDIA_ROOT as the full path to a directory where you'd like Django to store uploaded files. 在您的设置文件中,您需要将MEDIA_ROOT定义为您希望Django存储上载文件的目录的完整路径。 (For performance, these files are not stored in the database.) Define MEDIA_URL as the base public URL of that directory. (为了提高性能,这些文件未存储在数据库中。)将MEDIA_URL定义为该目录的基本公共URL。 Make sure that this directory is writable by the Web server's user account. 确保该目录可由Web服务器的用户帐户写入。

Add the FileField or ImageField to your model, defining the upload_to option to specify a subdirectory of MEDIA_ROOT to use for uploaded files. 将FileField或ImageField添加到模型中,定义upload_to选项,以指定用于上载文件的MEDIA_ROOT的子目录。

All that will be stored in your database is a path to the file (relative to MEDIA_ROOT). 将存储在数据库中的所有文件都是该文件的路径(相对于MEDIA_ROOT)。 You'll most likely want to use the convenience url attribute provided by Django. 您很可能想使用Django提供的便捷url属性。 For example, if your ImageField is called mug_shot, you can get the absolute path to your image in a template with {{ object.mug_shot.url }}. 例如,如果您的ImageField名为mug_shot,则可以使用{{object.mug_shot.url}}在模板中获取图像的绝对路径。

Also you need to modify urls to serve media files during development . 另外,您还需要修改URL以在开发过程中提供媒体文件。

So you need to add MEDIA_ROOT and MEDIA_URL in your settings file: 因此,您需要在设置文件中添加MEDIA_ROOTMEDIA_URL

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

Add to urls file this: 添加到URL文件:

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

and add url method inside template: 并在模板内添加url方法:

<img src="{{item.item_logo.url}}">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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