我正在尝试在我的主页上随机显示视频。用户能够上载视频,该视频将被保存到媒体/文档中的文档以及数据集中。我尝试了下面的代码,但它一直给我一个错误,
Exception Type: TemplateSyntaxError at /
Exception Value: 'media' is not a registered tag library. Must be one of:
admin_list
admin_modify
admin_static
admin_urls
cache
i18n
l10n
log
static
staticfiles
tz
我从urls.py中删除了settings.DEBUG:
,并将.url
添加到了{% media 'doc.document.url' %}
,但这是行不通的。
home.html
{% load media %}
{% for doc in document %}
<video width='320' height= '240' controls>
<source src="{% media 'doc.document.url' %}" type='video/mp4'>
Your browser does not support the video tag.
</video>
{% endfor %}
models.py
from django.db import models
from datetime import datetime
from django.contrib.auth.models import User
from django.conf import settings
...
class Document(models.Model):
title = models.CharField(max_length=100, default='NoTitle')
description = models.CharField(max_length=255, blank=True)
document = models.FileField(upload_to='documents/')
uploaded_at = models.DateTimeField("date published")#(auto_now_add=True)
creator = models.ForeignKey('auth.User', on_delete=models.CASCADE, blank=True, null=True)
def __str__(self):
return self.title
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path("", views.homepage, name="homepage"),
path("myconta/", views.myconta, name="myconta"),
path("upload/", views.model_form_upload, name="upload"),
path("register/", views.register, name="register"),
path('', include("django.contrib.auth.urls")),
]
#if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
views.py
def homepage(request):
return render(request=request, template_name="main/home.html", context={"sites": Info.objects.all})
return render(request=request, template_name="main/home.html", context={"document": Document.objects.all})
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main.apps.MainConfig',
]
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
目录
MySite
-main
-__pycache__
-migrations
-static
-images
main.css
-templates
[all my html code]
init
admin.py
apps.py
forms.py
models.py
tests.py
urls.py
views.py
-media
-documents
-mysite
-pycache
init.py
settings.py
urls.py
wsgi.py
db.sqlite3
答案 0 :(得分:0)
home.html
{% load media %}
{% for doc in document %}
<video width='320' height= '240' controls>
<source src="{{ MEDIA_URL }} {{ doc.document.url }}" type='video/mp4'>
Your browser does not support the video tag.
</video>
{% endfor %}
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_PATH = os.path.join(BASE_DIR, '/media/')