Embedding Videos in my Django site

三世轮回 提交于 2019-12-22 18:24:06

问题


I have no idea about embedding videos in Django. I am storing my videos in a database. I need to do embedding, displaying and deleting my videos in my Django site.

Can any one give a right direction for do this.

Thanks in advance.


回答1:


Hi this question is a little tricky.

Django is basically a framework for creating web apps using python, you are saying you already have a Django website and you are not explaining what you have, also not specifying what format you have for your videos (e.g.: binary? or folder paths? or urls?) so there are plenty options.

Here is a hint, what about creating a SPA (single page architecture) HTML/CSS3/JS with angularJS to make calls to your API/website (django)

  • Create folder name called "static" in your root django project folder
  • Create folder name called "templates" in your root django project folder
  • Inside "static" add following structure
    • images
    • js
    • css
  • Inside "templates" add your html file
    • index.html

1) Go to your settings.py and add following paths

  • Create 3 static config vars for your HTML (templates) and CSS/JS/IMG/VIDEOS (static) folders

    STATICFILES_DIRS=['/your_home_path/your_project/static/']
    STATIC_URL = '/static/'
    TEMPLATES_PATH = '/your_home_path/your_project/templates/'
    
  • Modify your TEMPLATES static var to use TEMPLATES_PATH

    TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATES_PATH],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
    

    ]

2) Go to you urls.py file and register your template HTML file so it can be used as the website layout also create a new route for test a new controller/action

url(r'^$', TemplateView.as_view(template_name="index.html")),
url(r'testingwebsite/v1/api/test/action$', action_controller_name),

3) Go to your views.py file and add controller action code so you can do whatever you want (GET/UPDATE/DELETE a video)

def action_controller_name(request):
    #Your_code_here: execute py scripts or call API or connect to DB
    #return json
    return HttpResponse(out, content_type="application/json")

4) Include required libraries in your views.py

from django.http import HttpResponse
#static files import
from django.conf import settings
from django.conf.urls.static import static

If you have videos on your database in binary format

Read them from the database and create a temp file in a temp folder so you can add video html tag to your django template

If instead you have urls or file paths of this videos

Read them from the database so you can add video html tag to your django template

How to include static folder's content files(videos/images/css/js/etc) in your HTML?

{% static "images/logo.png" %}
{% static "videos/myvideo.mp4" %}

Video tag sample

<video width="320" height="240" controls>
  <source src="{% static "videos/myvideo.mp4" %}" type="video/mp4">
  <source src="{% static "videos/myvideo.ogg" %}" type="video/ogg">
Your browser does not support the video tag.
</video>

Img tag sample

<img alt="majordomo_logo" src="{% static "images/logo.png" %}" class="img-circle" width="60">

**You could do the same for a video*

How to trigger action of your angularJS app/controller/service on your HTML?

  • Create a function "selectEntity()" inside your controller then call it from your HTML like this:

     <button type="button" class="btn btn-theme" ng-click="selectEntity()">Off</button>
    

How to use static files on your python code? e.g.: execute python script inside "static" folder

command="bash " + settings.STATICFILES_DIRS[0] + "pyscripts/clean-memory-mock.sh"

p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

out, err = p.communicate()

print out

Hope this helps!



来源:https://stackoverflow.com/questions/39489526/embedding-videos-in-my-django-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!