Django Wagtail嵌入视频不显示:画廊页面点击后视频无法加载求助
Hey there! Let's work through why your videos aren't showing up after clicking into the HomePageGallery page. Here are the most common areas to check and fix:
Verify your Model's video field setup
Make sure you're using the correct field type for storing videos and that the upload path is configured properly. For example, in yourmodels.py:class HomePageGallery(models.Model): # Add other necessary fields (like thumbnail, title, etc.) video = models.FileField(upload_to='gallery_videos/', blank=True, null=True) # Don't forget to run migrations after making changes!Double-check that you've run
python manage.py makemigrationsandpython manage.py migrateto apply any field changes.Confirm your View passes video data to the template
Ensure your view is fetching the correct HomePageGallery instance and passing it to the template context. For a function-based view:def gallery_detail(request, gallery_id): gallery_item = HomePageGallery.objects.get(id=gallery_id) return render(request, 'home_page_gallery.html', {'gallery_item': gallery_item})For class-based views, make sure you're using
DetailViewwith the correct model and template name.Fix your template's video rendering code
Inhome_page_gallery.html, you need to correctly reference the video's URL and use a valid HTML5<video>tag. Here's a reliable example:{% if gallery_item.video %} <div class="video-container"> <video controls width="640" height="360"> <source src="{{ gallery_item.video.url }}" type="video/mp4"> <!-- Add other source tags for different formats if needed --> Your browser doesn't support HTML5 video playback. </video> </div> {% else %} <p>No video has been added to this gallery item yet.</p> {% endif %}Make sure you're using the same variable name (
gallery_itemin this case) that you passed from your view.Check your media file configuration
If the video file is uploaded but not accessible, your Django media settings might be misconfigured. Insettings.py:MEDIA_ROOT = BASE_DIR / 'media' MEDIA_URL = '/media/'And in your root
urls.py, add this to serve media files in development:from django.conf import settings from django.conf.urls.static import static urlpatterns = [ # Your existing URL patterns ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)Note: For production, you'll need to configure your web server (like Nginx or Apache) to serve media files instead of relying on Django's static helper.
Debug with browser developer tools
Open your browser's dev tools (press F12), go to the Network tab, and reload the page. Look for your video file in the requests—if it returns a 404 error, the path in your template is incorrect. Check the Console tab for any JavaScript or HTML errors that might block video rendering.Validate file existence and permissions
Navigate to themedia/gallery_videos/directory (or whateverupload_topath you set) on your server. Confirm the video file exists there, and that the server has read permissions for the file and directory.
内容的提问来源于stack exchange,提问作者Adrian Covaci

