我正在尝试创建我的第一个自定义模板标记。 我的模型看起来像这样:
from django.db import models
from django.contrib import admin
from audiofield.fields import AudioField
# Create your models here.
class Musikk(models.Model):
tittel = models.CharField(max_length=20, default = "Skriv tittel her")
artist = models.CharField(max_length=20, default = "Artist")
cover = models.FileField("Cover", upload_to="audio/")
audio = AudioField(upload_to="audio/", blank=True, ext_whitelist=(".mp3",".wav",".ogg"))
is_active = models.BooleanField(("is active"), default = False)
date_created = models.DateField(verbose_name="Created on date", auto_now_add="True")
class MusikkAdmin(admin.ModelAdmin):
list_display = ('tittel', 'artist', 'cover', 'audio', 'is_active', 'date_created')
我的settings.py是这样的:
# Media files
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
我的模板标签(musikk_tag.py)如下所示:
from django import template
from .. import models
register = template.Library()
@register.assignment_tag
def get_musikk_tags():
return models.Musikk.objects.all()
我确实要渲染'标题'和#'艺术家'从模型到我的html,但不是我的'封面'和' audio'。
不知何故该文件未上传到/ media /。请帮我弄清楚什么是错的!