图像未显示在 Django 网站的滑块中

问题描述 投票:0回答:1

我已完成显示 Django 网站项目的图像滑块的所有必要步骤。但是图像没有显示而没有显示任何错误,我认为我的 home.html 文件 Django 代码中有一个错误。为此指导我。谢谢大家的期待

模型.py

from django.db import models
from datetime import datetime
from ckeditor.fields import RichTextField
from multiselectfield import MultiSelectField

# Create your models here.
class Car(models.Model):
    state_choice=(
        ('PU', 'Punjab'),
        ('KPK', 'Khyber PukhtunKha'),
        ('BL', 'Balochestan'),
        ('SN', 'Sindh'),
        ('FD', 'Federal Territory'),
    )
    TRANSMISSION_CHOICES = (
        ('M', 'Manual'),
        ('A', 'Automatic'),
    )
    door_choices = (
        ('2', '2'),
        ('3', '3'),
        ('4', '4'),
        ('5', '5'),
        ('6', '6'),
    )

    features_choices=(
        ('Cruise Control', 'Cruise Control'),
        ('Audio Interface', 'Audio Interface'),
        ('Airbags', 'Airbags'),
        ('Air Conditioning', 'Air Conditioning'),
        ('Seat Heating', 'Seat Heating'),
        ('Alarm System', 'Alarm System'),
        ('ParkAssist', 'ParkAssist'),
        ('Power Steering', 'Power Steering'),
        ('Reversing Camera', 'Reversing Camera'),
        ('Direct Fuel Injection', 'Direct Fuel Injection'),
        ('Auto Start/Stop', 'Auto Start/Stop'),
        ('Wind Deflector', 'Wind Deflector'),
        ('Bluetooth Handset', 'Bluetooth Handset'),
    )

    year_choice = []
    for r in range(2000, (datetime.now().year+1)):
        year_choice.append((r,r))
    car_title = models.CharField(max_length=255)
    state = models.CharField(choices=state_choice, max_length=100)
    city = models.CharField(max_length=100)
    color = models.CharField(max_length=100)
    model = models.CharField(max_length=100)
    year = models.IntegerField(('year'), choices=year_choice)
    condition = models.CharField(max_length=100)
    price = models.IntegerField()
    description = RichTextField()
    car_photo = models.ImageField(upload_to='photos/%Y/%m/%d/')
    car_photo_1 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
    car_photo_2 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
    car_photo_3 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
    car_photo_4 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
    features = MultiSelectField(choices=features_choices, max_length=255)
    body_style = models.CharField(max_length=100)
    engine = models.CharField(max_length=100)
    transmission = models.CharField(max_length=100)
    interior = models.CharField(max_length=100)
    miles = models.IntegerField()
    doors = models.CharField(choices=door_choices, max_length=10)
    passengers = models.IntegerField()
    vin_no = models.CharField(max_length=100)
    milage = models.IntegerField()
    fuel_type = models.CharField(max_length=50)
    no_of_owners = models.CharField(max_length=100)
    is_featured = models.BooleanField(default=False)
    created_date = models.DateTimeField(default=datetime.now, blank=True)

views.py

from django.shortcuts import get_object_or_404, render
from cars.models import Car
# Create your views here.
def cars(request):
    cars = Car.objects.order_by('-created_date')
    data = {
        'cars': cars,
        }
    return render(request, 'cars/cars.html', data)
def car_detail(request, id):
    single_car = get_object_or_404(Car, pk=id)
    data = {
        'single_car': single_car,
    }

    all_cars = Car.objects.order_by('-created-date')
    data = {
        'all_cars' : all_cars,
    } 

    return render(request, 'cars/car_detail.html', data)

home.html 我认为 home.html 中的 Django 代码很容易出错。在这方面提供帮助。

<div class="featured-car content-area">
    <div class="container">
        <!-- Main title -->
        <div class="main-title">
            <h1>Latest <span>Cars</span></h1>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
        </div>
        <div class="row">
            {% for car in all_cars %}
            <div class="col-lg-4 col-md-6">
                <div class="car-box">
                    <div class="car-thumbnail">
                        <a href="{{car.car_photo}}" class="car-img">
                            <div class="tag">For Sale</div>
                            <img class="d-block w-100" src="{{car.car_photo.url}}" alt="car">
                            <div class="facilities-list clearfix">
                                <ul>
                                    <li>
                                        <span><i class="flaticon-way"></i></span>4,000 km
                                    </li>
                                    <li>
                                        <span><i class="flaticon-calendar-1"></i></span>2020
                                    </li>
                                    <li>
                                        <span><i class="flaticon-manual-transmission"></i </span>Manual
                                    </li>
                                </ul>
                            </div>
                        </a>
                        <div class="carbox-overlap-wrapper">
                            <div class="overlap-box">
                                <div class="overlap-btns-area">
                                    <div class="car-magnify-gallery">
                                        {% if car.car_photo_1 %} 
                                        <a href="{{car.photo_1}}" class="overlap-btn">
                                            <i class="fa fa-expand"></i>
                                            <img class="hidden" src="{{ car.car_photo_1.url }}">
                                        </a>
                                        {% endif %}
                                        {% if car.car_photo_2 %}
                                        <a href="{{ car.car_photo_2 }}" class="hidden">
                                            <img class="hidden" src="{{ car.car_photo_2.url }}">
                                        </a>
                                        {% endif %}
                                        {% if car.car_photo_3 %}
                                        <a href="{{ car.car_photo_3 }}" class="hidden">
                                            <img class="hidden" src="{{ car.car_photo_3.url }}">
                                        </a>
                                        {% endif %}
                                        {% if car.car_photo_4 %}
                                        <a href="{{ car.car_photo_4 }}" class="hidden">
                                            <img class="hidden" src="{{ car.car_photo_4.url }}">
                                        </a>
                                        {% endif %}
                                        {% if car.car_photo_5 %}
                                        <a href="{{ car.car_photo_5 }}" class="hidden">
                                            <img class="hidden" src="{{ car.car_photo_5.url }}">
                                        {% endif %}   
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="detail">
                        <h1 class="title">
                            <a href="car-details.html">Lamborghini Huracán</a>
                        </h1>
                        <div class="location">
                            <a href="car-details.html">
                                <i class="flaticon-pin"></i>123 Kathal St. Tampa City,
                            </a>
                        </div>
                    </div>
                    <div class="footer clearfix">
                        <div class="pull-left ratings days">
                            <p class="cartype">Sedan</p>
                        </div>
                        <div class="pull-right">
                            <p class="price">$850.00</p>
                        </div>
                    </div>
                </div>
            </div>
            {% endfor %}
        </div>
    </div>
</div>
django django-models bootstrap-4 django-views django-templates
1个回答
0
投票

我做了一个类似的项目,一切正常。这些是我遵循的步骤

installed_app
中添加汽车应用程序 在设置中添加
MEDIA_ROOT
MEDIA_URL

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

在网址中添加

MEDIA_ROOT
MEDIA_URL

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # you urls here
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

而且你也有几个错误

视图

car_detail
替换

all_cars = Car.objects.order_by('-created-date')

all_cars = Car.objects.order_by('-created_date')

以及在您为汽车定义

<a></a>
的模板中

<a href="{{car.car_photo}}" class="car-img">
...
</a>

您必须像这样在 href 中传递 url ...

并基于 MDN 文档处理文件

还建议用连字符分隔单词,而不是 下划线:my-file.html 与 my_file.html。

所以你的html文件应该是

car-detail.html
而不是
car_detail.html

© www.soinside.com 2019 - 2024. All rights reserved.