Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
103 views
in Technique[技术] by (71.8m points)

python - Multi-image is my motive in this project. I wants to show images but not able to show them on front-end but able to upload multi-image on admin panel

My project is based on the multi-image there is problem with html file. There is some mistakes with the html file please help to find the problem in it. I am able to upload the images but they are not able to show on the frontend. help me in my project. my html files are thus: base.html

<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha2/css/bootstrap.min.css" integrity="sha384-DhY6onE6f3zzKbjUPRc2hOzGAdEf4/Dz+WJwBvEYL/lkkIsI3ihufq9hk9K4lVoK" crossorigin="anonymous">
    <title>Multi Image Tutorial</title>
</head>
<body>
    <div class="container py-4">
        {% block content %}
        {% endblock %}
    </div>
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>

blog.html

{% extends 'base.html' %}
{% block content %}
<div class="row row-cols-1 row-cols-md-2">
    {% for post in posts %}
    <div class="col mb-4">
        <div class="card">
            <div class="view overlay">
                <img class="card-img-top" src="{{post.image.url}}"
                    alt="Card image cap" >
                <a href="#">
                <div class="mask rgba-white-slight"></div>
                </a>
            </div>
            <div class="card-body">
                <h4 class="card-title">{{post.title}}</h4>
                <p class="card-text">{{post.description}}</p>
                <a href="{% url 'detail' post.id %}" class="btn btn-primary btn-md">Read More</a>
            </div>
        </div>
    </div>
    {% endfor %}
</div>
{% endblock %}

detail.html

{% extends 'base.html' %}
{% block content %}
<div id="carouselExampleIndicators" class="carousel slide" data-mdb-ride="carousel">
  <ol class="carousel-indicators">
    {% for p in photos %}
    <li data-mdb-target="#carouselExampleIndicators" data-mdb-slide-to="{{forloop.counter0}}" class="{% if forloop.counter0 == 0 %} active {% endif %}"></li>
    {% endfor %}
</ol>
  <div class="carousel-inner" role="listbox">
    {% for p in photos %}
    <div class="carousel-item {% if forloop.counter0 == 0 %} active {% endif %}">
      <img
        src="{{post.image.url}}"
        class="d-block w-100"
        alt="First slide"/>
    </div>
    {% endfor %}
</div>
  <a
    class="carousel-control-prev"
    href="#carouselExampleIndicators"
    role="button"
    data-mdb-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="visually-hidden">Previous</span>
  </a>
  <a 
    class="carousel-control-next"
    href="#carouselExampleIndicators"
    role="button"
    data-mdb-slide="next" >
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="visually-hidden">Next</span>
  </a>
</div>
{% endblock %}

models.py

from django.db import models    
class Post(models.Model):
    title           = models.CharField(max_length=120)
    description     = models.TextField()
    image           = models.ImageField(upload_to='products/', null=True, blank=True)
    def __str__(self):
        return self.title
class PostImage(models.Model):
    post = models.ForeignKey(Post, default=None, on_delete=models.CASCADE)
    image = models.ImageField(upload_to='images/')
    def __str__(self):
        return self.post.title

views.py

from django.shortcuts import render, get_object_or_404
from .models import Post, PostImage

def blog_view(request):
    posts = Post.objects.all()
    return render(request, 'blog.html', {'posts':posts})
def detail_view(request, id):
    post = get_object_or_404(Post, id=id)
    photos = PostImage.objects.filter(post=post)
    return render(request, 'detail.html', {
        'post':post,
        'photos':photos,
    })

.......

question from:https://stackoverflow.com/questions/65896239/multi-image-is-my-motive-in-this-project-i-wants-to-show-images-but-not-able-to

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...