Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024/3/11_views #3

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added IMG_3657.mp4
Binary file not shown.
Binary file added IMG_3657TEMP_MPY_wvf_snd.mp3
Binary file not shown.
8 changes: 8 additions & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from pathlib import Path
from django.core.management.utils import get_random_secret_key
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand Down Expand Up @@ -126,3 +127,10 @@
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# 動画ファイルのルート
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


8 changes: 6 additions & 2 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [
path('admin/', admin.site.urls),
]
path('', include('dd_app.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
7 changes: 7 additions & 0 deletions dd_app/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path, include
from .views import Rendering_View, DetailVideoView

urlpatterns = [
path('', Rendering_View.as_view(), name='home'),
path('detail/<int:pk>/', DetailVideoView.as_view(), name='detail')
]
64 changes: 63 additions & 1 deletion dd_app/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,65 @@
from django.db.models.base import Model as Model
from django.db.models.query import QuerySet
from django.shortcuts import render
import re
from django.views.generic import View ,DetailView
from .models import Video
import re
from moviepy.editor import VideoFileClip
from django.core.files.storage import default_storage

# Create your views here.


class Rendering_View(View):
def get(self, request, *args, **kwargs):
video_ids = []
video_files = []
video_titles =[]
video_pks = []
videos = Video.objects.all()

for video in videos:
video_titles.append(video.title)
video_pks.append(video.pk)

if video.video_file:
# video_file = MOV_Conversion(video.video_file)

video_ids.append(video.video_file)

else:
video_id_match = re.search(r'(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})', video.video_url)
video_ids.append(video_id_match.group(1))

ziplist = zip(video_ids, video_titles, video_pks)

return render(request, 'youtube_embed.html', {'ziplist': ziplist})


#詳細画面
class DetailVideoView(DetailView):
template_name = 'detail.html'
model = Video

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
video = self.object
youtube_video_id = self.get_youtube_video_id(video.video_url)
context['youtube_video_id'] = youtube_video_id
return context

def get_youtube_video_id(self, url):
video_id_match = re.search(r'(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})', url)
if video_id_match:
return video_id_match.group(1)
return None










1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ django-taggit==5.0.1
pillow==10.2.0
sqlparse==0.4.4
typing_extensions==4.9.0
# moviepy
Empty file removed templates/Show_video_detail.html
Empty file.
13 changes: 13 additions & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
{% block content %}
{% endblock content%}
</body>
</html>
21 changes: 21 additions & 0 deletions templates/detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h3>{{ object.title }}</h3>
{% comment %} {% if %} {% endcomment %}
<iframe width="560" height="315" src="https://www.youtube.com/embed/{{ youtube_video_id }}" frameborder="0" allowfullscreen></iframe>
{% comment %} {% else %} {% endcomment %}
<p>Invalid YouTube URL</p>
{% comment %} {% endif %} {% endcomment %}
<p>{{ object.likes }}</p>
<p>{{ object.uploaded_at }}</p>
<p>{{ object.description }}</p>
<p>{{ object.views }}</p>
</body>
</html>
33 changes: 33 additions & 0 deletions templates/youtube_embed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h2>Embedded YouTube Video</h2>
{% comment %} {% if video_ids %} {% endcomment %}
{% for id, title, pk in ziplist %}

{% comment %} {% if id == 'videos/mp4' %} {% endcomment %}
<video controls width="80%">
<source src="./media/videos/video1371386122.mp4" type="video/mp4">
</video>

{% comment %} {% else %} {% endcomment %}

<iframe width="560" height="315" src="https://www.youtube.com/embed/{{ id }}" frameborder="0" allowfullscreen></iframe>
<h4>{{ title }}</h4>
<a href="{% url 'detail' pk=pk %}">詳細へ</a>

{% comment %} {% endif %} {% endcomment %}
{% endfor %}
{% comment %} {% else %} {% endcomment %}
{% comment %} <p>Invalid YouTube URL</p> {% endcomment %}
{% comment %} {% endif %} {% endcomment %}
</body>
</html>