簡體   English   中英

Django模板中的ImageField變量

[英]ImageField variable in Django Template

因此,我已經遍歷了Django中相應模板中的一個對象變量,除image變量之外,每個項目都很好地遍歷了。 我不確定自己在做什么錯。 但是這是我的代碼:

模型:查看我將上傳的圖像文件路由到的位置。

class Car(models.Model):

    def fileLocation(instance, filename):
        return 'media/cars/{0}/{1}'.format(instance.agent.username, os.path.basename(filename))

    make = models.CharField(max_length=100)
    model = models.CharField(max_length=100)
    year = models.IntegerField()
    car_registration = models.CharField(max_length=100, unique=True) 
    insurance_exp = models.DateField(max_length=100)
    cost_per_day = models.IntegerField()
    description = models.TextField(max_length=1000) 
    agent = models.ForeignKey(Agent, on_delete=models.CASCADE)
    image1 = models.ImageField(upload_to=fileLocation)
    image2 = models.ImageField(upload_to=fileLocation)
    image3 = models.ImageField(upload_to=fileLocation)
    image4 = models.ImageField(upload_to=fileLocation)
    image5 = models.ImageField(upload_to=fileLocation)
    available = models.BooleanField()
    added_date = models.DateField(default = timezone.now()) 

    def __str__(self):
        return self.model

視圖:

from django.shortcuts import render
from .models import Car, Agent

# Create your views here.
def index(request):
    all_cars = Car.objects.all()
    context = {'all_cars':all_cars}
    return render(request, "home.html", context)

模板(home.html):

{% extends 'basic.html' %}

{% block content %}
<div class="container">
    <div class="row ">

    {% for car in all_cars %}
    <div class="col-md-8 col-md-offset-2 list-cars">
        <div class="media">


          <div class="media-left media-top">

        <a href="#">
             <img src="{{car.image1.url}}">

           </a>
          </div>

          <div class="media-body">
            <h4 class="media-heading">{{car.make}} {{car.model}}</h4>
            <p>{{car.description}} </p>
            <p class="price">Kshs {{car.cost_per_day}} Per Day</p>

            {% if car.available == True %}
                <button class="btn btn-success">Available</button>
            {% else %}
                <button class="btn btn-danger">Not Available</button>
                <button class="btn btn-default">When will it be available?</button>
            {% endif %}
          </div>
        </div>
        </div>
        {% endfor %}



    </div>
</div>  




{% endblock %}

SETTINGS.PY:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = '/static/'

STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

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

網址

from django.conf.urls import url, include
from django.contrib import admin
from cars import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('cars.urls')),
]

結果: 圖像無法正確渲染

我的圖像路徑有問題嗎?

得到答案:我只是將“ MEDIA”文件夾添加到靜態文件夾中,然后將圖像的URL更改為從“ STATIC”開始。 這些圖像可以完美地顯示在站點上。

class Car(models.Model):

def fileLocation(instance, filename):
    return 'static/media/cars/{0}/{1}'.format(instance.agent.username, os.path.basename(filename))

make = models.CharField(max_length=100)
model = models.CharField(max_length=100)
year = models.IntegerField()
car_registration = models.CharField(max_length=100, unique=True) 
insurance_exp = models.DateField(max_length=100)
cost_per_day = models.IntegerField()
description = models.TextField(max_length=1000) 
agent = models.ForeignKey(Agent, on_delete=models.CASCADE)
image1 = models.ImageField(upload_to=fileLocation)
image2 = models.ImageField(upload_to=fileLocation)
image3 = models.ImageField(upload_to=fileLocation)
image4 = models.ImageField(upload_to=fileLocation)
image5 = models.ImageField(upload_to=fileLocation)
available = models.BooleanField()
added_date = models.DateField(default = timezone.now()) 

在函數fileLocation()上,我添加了返回值“ static / ....”。 然后繼續將媒體文件夾嵌入到靜態文件夾中。 而且有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM