我摆弄了 django ImageField
,
例如,我得到了以下模型
class Card(models.Model):
name = models.TextField()
mana = models.IntegerField()
rarity = models.IntegerField(choices=RARITY_CHOICES)
card_class = models.IntegerField(choices=CLASS_CHOICES)
card_set = models.IntegerField(choices=SET_CHOICES)
card_type = models.IntegerField(choices=TYPE_CHOICES)
def __unicode__(self):
return self.name
class Weapon(Card):
damage = models.already created a deck with some cards in the django admin interface. Now finally I wanted to render it with a custom view in my template with this viewIntegerField()
durability = models.IntegerField()
image = models.ImageField(upload_to='/home/ubuntu/illuminated/media/images/weapons/')
class Deck(models.Model):
name = models.CharField(max_length=20)
spell_cards = models.ManyToManyField(Spell, blank=True)
weapon_cards = models.ManyToManyField(Weapon, blank=True)
minion_cards = models.ManyToManyField(Minion, blank=True)
我已经在 django 管理界面中创建了一个带有一些卡片的卡片组。现在最后我想用这个视图在我的模板中用一个自定义视图来渲染它
class ShowDeck(TemplateView):
template_name = 'hsguides/deck.html'
def get_context_data(self, **kwargs):
context = super(ShowDeck, self).get_context_data(**kwargs)
context['deck'] = Deck.objects.all()
return context
这个简单的模板已经在 django 管理界面中创建了一个包含一些卡片的卡片组。现在最后我想用这个视图在我的模板中用一个自定义视图来渲染它
{% for foo in deck.all %}
{{ foo.name }}
<br>
{% for weapon in foo.weapon_cards.all %}
{{ weapon.name }}
<img src="{{ weapon.image }}" height="{{ weapon.image.height }}" width="{{ weapon.image.width }}">
{% endfor %}
{% endfor %}
名称正在呈现,当我查看页面源时,图像 url、宽度和高度也是如此,但图像根本不显示。
当我在浏览器中单击查看图像时,我看到以下错误
使用在Illuminated.urls 中定义的URLconf,Django 按以下顺序尝试了这些URL 模式:
^admin/ ^account/ ^guides/
当前的 URL,home/ubuntu/illuminated/media/images/weapons/Truesilver_Champion_Gold_ktmWGK8.png,与其中任何一个都不匹配
目前看起来像这样
页面源看起来像这样
Basic Paladin
<br>
Truesilver Champion
<img src="/home/ubuntu/illuminated/media/images/weapons/Truesilver_Champion_Gold_ktmWGK8.png" height="396" width="289">
任何形式的帮助都非常感谢!