【问题标题】:Django - User can upload an image, but the image does not display - HTML issue? (Beginner help)Django - 用户可以上传图片,但图片不显示 - HTML 问题? (初学者帮助)
【发布时间】:2021-01-01 23:23:36
【问题描述】:

我是 Django 新手,正在为用户创建一种将图像上传到帖子的方法。然后图像应该在每个帖子的页面上可见。

到目前为止,在我的“创建”功能中,用户可以成功创建帖子并上传照片。照片已上传到我的项目中,但仍然没有显示。非常感谢任何帮助。

我认为问题在于views.py中的类,以及index.html中的html是怎么写的。只是不知道如何解决。

views.py 创建帖子:

def create(request):
    if request.method == 'GET':
        context = {
            'form': CreateForm(),
            'form1': CategoryForm(),
            'img_form': ImgForm(),
        }
        return render(request, "auctions/create.html", context)
    else:
        form = CreateForm(request.POST, request.FILES)
        form1 = CategoryForm(request.POST, request.FILES)
        img_form = ImgForm(request.POST, request.FILES)

        if form.is_valid():
            title = form.cleaned_data['title']
            description = form.cleaned_data['description']
            starting_bid = form.cleaned_data['starting_bid']
        if form1.is_valid():
            category = form1.cleaned_data['category']
        if img_form.is_valid():
            image = img_form.cleaned_data['image']

            auctionCreated = Listings.objects.create(
                title=title,
                description=description,
                starting_bid=starting_bid,
            )
            categoryCreated = Categories.objects.create(
                category=category,
            )
            ImageCreated = Img.objects.create(
                image=image,
            )
            return redirect('index')

views.py - 这应该显示每个帖子的列表(在主页上),每个图像应该是可见的,但图像没有出现:

def index(request):
    items = Listings.objects.all()
    images = Img.objects.all()
    return render(request, "auctions/index.html", {
        'items': items,
        'images': images,
    })

views.py 显示图片:

class ImgDisplay(DetailView):
    model = Img
    template_name = 'listing.html', 'index'
    context_img_name = 'pic'

models.py

class Img(models.Model):
    image = models.ImageField(upload_to='images/')

forms.py

class ImgForm(ModelForm):
    class Meta:
        model = Img
        fields = '__all__'

index.html

    {% for i in items %}
        {% for photo in images %}
        <img src="{{pic.image.url}}" class="card-img" alt="...">
        {% endfor %}

          <a href="{% url 'listingpage' i.id %}"><button class="btn btn-primary">Bid Now! 
     </button></a>

     {% endfor %}

【问题讨论】:

    标签: django django-models django-views django-forms django-templates


    【解决方案1】:

    您的 index.html 参数不正确。您从index view 传递images 作为上下文参数。但是当你在 index.html 中使用for-loop 时,你使用的是pic.image.url 作为图像源,它应该是photo.image.url

    如果你想使用ImgDisplay(DetailView)显示图像,那么你可以使用pic.image.url,并且还需要更正template_name

    `template_name` = 'listing.html', 'index.html'
    

    【讨论】:

      猜你喜欢
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 2017-10-12
      • 2018-07-20
      • 2017-09-04
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      相关资源
      最近更新 更多