如何在Django模板中访问和显示Cloudinary上传的图像

时间:2016-01-25 01:42:05

标签: python django image cloudinary

使用 框架:Django 1.9 语言:Python 3.4 开发服务器:Django 操作系统:Windows 10

我仍然是创建网站的新手。我一直在用django来学习。现在,我遇到了涉及Cloudinary的问题。我有一个模板,需要从Cloudinary检索和显示图像。但是,当我使用Cloudinary模板标记时,显示图像URL,但不显示图像。我需要显示图像。

有人可以帮忙吗?

型号:

from django.db import models
from cloudinary.models import CloudinaryField

from django.core.urlresolvers import reverse

class PostQuerySet(models.QuerySet):
    def published(self):
        return self.filter(publish=True)

class HeroImage(models.Model):
    image = CloudinaryField('image')
    title = models.CharField(max_length=200, default='')

    def __str__(self):
        return self.title

class Tag(models.Model):
    slug = models.SlugField(max_length=200, unique=True)

    def __str__(self):
        return self.slug

class Post(models.Model):

    STATUS_CHOICES = (
        ('d', 'Draft'),
        ('c', 'Complete'),
        ('r', 'Review'),
        ('p', 'Published'),
        ('w', 'Withdrawn'),
    )

    FACEBOOK_CHOICES = (
        ('web', 'website'),
        ('art', 'article'),
        ('blg', 'blog'),
        ('vid', 'video'),
        ('aud', 'audio'),
    )
    hero_image = models.ForeignKey(HeroImage)
    tag = models.ManyToManyField(Tag)
    featured_image = CloudinaryField('image', default='')
    page_title = models.CharField(max_length=70, default='')
    page_description = models.CharField(max_length=155, default='')
    canonical = models.URLField(default='')
    situation = models.TextField()
    breakdown = models.TextField()
    review = models.TextField()
    slug = models.SlugField(max_length=200, unique=True, default='')
    publish = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=1, choices=STATUS_CHOICES, default='')
    og_title = models.CharField(max_length=88, default='')
    og_type = models.CharField(max_length=3, choices=FACEBOOK_CHOICES, default='')
    og_description = models.CharField(max_length=200, default='')
    og_url = models.URLField(default='')

    objects = PostQuerySet.as_manager() 

    def __str__(self):
        return self.page_title

    def get_absolute_url(self):
        return reverse("post_detail", kwargs={"slug": self.slug})

    class Meta:
        verbose_name = "Post"
        verbose_name_plural = "Posts"
        ordering = ["-created"]

查看:

from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic

from django import forms

from django.http import HttpResponse

from cloudinary.forms import cl_init_js_callbacks  

from . import models 

class PostIndex(generic.ListView):
    queryset = models.Post.objects.published()
    template_name = 'index.html'

class PostDetail(generic.DetailView):
    model = models.Post
    template_name = "post.html"

模板:

的index.html

{% block blog_posts %}
{% for object in object_list %}
{% load cloudinary %}
  <div class="post">
    <h2>{{ object.page_title }}</h2>
    {% cloudinary "shq7cw9kjufj8zhj1rni.png" %}
    <p class="meta">{{ object.created }}</p>
    {{ object.situation }}
  </div>
{% endfor %}
{% endblock %}

post.html

<div class="post">
    <h2><a href="{% url "post_detail" slug=object.slug %}">{{ object.title }}</a></h2>
    <p class="meta">
        {{ object.created }} | Tagged under {{  object.tags.all|join:", " }}
    </p>
    <div>
        {{ object.situation }}
    </div>
    <div>
        {{ object.breakdown }}
    </div>
    <div>
        {{ object.review }}
    </div>
</div>

1 个答案:

答案 0 :(得分:2)

您可以尝试点击该链接,查看它带您的图像天气。或者检查图像保存的名称,有时它们不同。 本网站有详细说明http://cloudinary.com/documentation/django_integration