0

Есть модель пользователя Profile связанная с моделью User и модель Videofile связанная с Profile при помощи ForeignKey. Я пытаюсь вывести список всех пользователей и видеофайлы принадлежащие каждому пользователю. Получается вывести только имена и ссылку на видео, не понятно как сделать вывод видео в шаблон

user.models.py

class Profile(models.Model):

    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
    nickname = models.CharField(max_length=30, blank=True, verbose_name="Никнэйм")        
    userpic = models.ImageField(upload_to='userpics/', blank=True, null=True)

videofile.models.py

class VideoFile(models.Model):
    name = models.CharField(max_length=200,blank=True)
    file = models.FileField(upload_to="vstories/%Y/%m/%d", validators=[validate_file_extension])
    date_upload = models.DateTimeField(auto_now_add = True, auto_now = False, blank=True, null = True)
    descriptions =  models.TextField(max_length=200)
    reports = models.BooleanField(default=False)
    vstories = models.ForeignKey(Profile, blank = True, null = True) 

views.py

def vstories (request):
    profiles = Profile.objects.all()        
    return render(request, "vstories/vstories.html", {'profiles':profiles}) 

templates.html

{% for profile in profiles %}
    {{ profile.nickname }}

        {% for videofile in profile.videofile_set.all %}
        <p>{{ videofile.file.url }}</p>
        {%  endfor %}

    {%  endfor %}

1 ответ 1

0

Что бы отобразить видео на странице, как вариант, можно использовать в шаблоне html тэг video:

{% for profile in profiles %}
    {{ profile.nickname }}

    {% for videofile in profile.videofile_set.all %}
        <video width="320" height="240" controls src="{{ videofile.file.url }}">
          Your browser does not support the video tag.
        </video>
    {%  endfor %}

{%  endfor %}

Документация.

1
  • То что надо, спасибо!
    – Jekson
    25 апр 2018 в 13:23

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.