老师好! 谢谢老师的课程,第一次可以坚持到现在。我成功创建了外链页面,但是不知道什么原因在添加videosub之后就打不开了。我尝试debug了两天,还是无法解决这个问题。希望老师能够帮忙解答。感激!
external_video.html
def post(self, request):
name = request.POST.get('name')
image = request.POST.get('image')
video_type = request.POST.get('video_type')
from_to = request.POST.get('source_type')
nationality = request.POST.get('nationality_type')
info = request.POST.get('info')
if not all([name, image, video_type, from_to, nationality, info]):
return redirect('{}?error={}'.format(reverse('external_video'), 'missing required field(s)'))
result = check_and_get_video_type(VideoType, video_type, 'invalid video type')
if result.get('code') != 0:
return redirect('{}?error={}'.format(reverse('external_video'), result['msg']))
result = check_and_get_video_type(FromType, from_to, 'invalid source type')
if result.get('code') != 0:
return redirect('{}?error={}'.format(reverse('external_video'), result['msg']))
result = check_and_get_video_type(NationalityType, nationality, 'invalid nationality')
if result.get('code') != 0:
return redirect('{}?error={}'.format(reverse('external_video'), result['msg']))
try:
Video.objects.create(
name=name,
image=image,
video_type=video_type,
from_to=from_to,
nationality=nationality,
info=info
)
except:
return redirect('{}?error={}'.format(reverse('external_video', 'object creation failed')))
return redirect(reverse('external_video'))
from django.urls import path
from .views.base import Index
from .views.auth import Login, Logout, AdminManager, UpdateAdminStatus
from .views.video import ExternalVideo, VideoSub, VideoCastView, CastDelete
urlpatterns = [
path('', Index.as_view(), name='dashboard_index'),
path('login', Login.as_view(), name='login'),
path('logout', Logout.as_view(), name='logout'),
path('admin/manager', AdminManager.as_view(), name='admin_manager'),
path('admin/manager/update/status', UpdateAdminStatus.as_view(), name='admin_update_status'),
path('video/external/', ExternalVideo.as_view(), name='external_video'),
path('video/videosub/<int:video_id>', VideoSub.as_view(), name='video_sub'),
path('video/cast', VideoCastView.as_view(), name='video_cast'),
path('video/cast/delete/<int:cast_id>/<int:video_id>', CastDelete.as_view(), name='cast_delete'),
]
external_video.html
<%inherit file="../base.html" />
<%! from django.shortcuts import reverse %>
<%def name="content()">
<%
from app.model.video import VideoType, FromType, NationalityType
%>
<h1>External Videos</h1>
<button class="btn btn-info open-btn" id="open-add-video-btn">Create</button><span>${error}</span>
<form id="video-edit-area" class="form-horizontal edit-area add-video-area" action="" method="post">${csrf_token}
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Video Name">
</div>
<div class="form-group">
<input type="text" name="image" class="form-control" placeholder="Image">
</div>
<div class="form-group">
<label>Video Type </label>
<select class="form-control video-select" name="video_type">
%for video_type in VideoType:
<option value="${video_type.value}">${video_type.label}</option>
%endfor
</select>
</div>
<div class="form-group">
<label>Video Source </label>
<select class="form-control video-select" name="source_type">
%for from_to in FromType:
%if from_to != FromType('upload'):
<option value="${from_to.value}">${from_to.label}</option>
%endif
%endfor
</select>
</div>
<div class="form-group">
<label>Nationality Type </label>
<select class="form-control video-select" name="nationality_type">
%for nationality_type in NationalityType:
<option value="${nationality_type.value}">${nationality_type.label}</option>
%endfor
</select>
</div>
<div class="form-group">
<textarea name="info" class="form-control" placeholder="Description"></textarea>
</div>
<button type="submit" class="btn btn-info">Add Video</button>
</form>
</%def>
错误信息
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/dashboard/video/external
Django Version: 3.0.1
Python Version: 3.7.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "/Users/admin/venvs/django-project/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/admin/venvs/django-project/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/admin/venvs/django-project/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/admin/venvs/django-project/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/admin/venvs/django-project/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "/Users/admin/projects/django-video-site/video/app/utils/permission.py", line 17, in wrapper
return func(self, request, *args, **kwargs)
File "/Users/admin/projects/django-video-site/video/app/dashboard/views/video.py", line 22, in get
return render_to_response(request, self.TEMPLATE, data=data)
File "/Users/admin/projects/django-video-site/video/app/libs/base_render.py", line 43, in render_to_response
return HttpResponse(mako_template.render(**result))
File "/Users/admin/venvs/django-project/lib/python3.7/site-packages/mako/template.py", line 476, in render
return runtime._render(self, self.callable_, args, data)
File "/Users/admin/venvs/django-project/lib/python3.7/site-packages/mako/runtime.py", line 883, in _render
**_kwargs_for_callable(callable_, data)
File "/Users/admin/venvs/django-project/lib/python3.7/site-packages/mako/runtime.py", line 920, in _render_context
_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File "/Users/admin/venvs/django-project/lib/python3.7/site-packages/mako/runtime.py", line 947, in _exec_template
callable_(context, *args, **kwargs)
File "dashboard_video____base_html", line 35, in render_body
<source code not available>
File "dashboard_video_external_video_html", line 103, in render_content
<source code not available>
File "/Users/admin/venvs/django-project/lib/python3.7/site-packages/django/urls/base.py", line 87, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Users/admin/venvs/django-project/lib/python3.7/site-packages/django/urls/resolvers.py", line 637, in _reverse_with_prefix
text_candidate_subs[k] = converters[k].to_url(v)
File "/Users/admin/venvs/django-project/lib/python3.7/site-packages/django/urls/converters.py", line 12, in to_url
return str(value)
File "/Users/admin/venvs/django-project/lib/python3.7/site-packages/mako/runtime.py", line 230, in __str__
raise NameError("Undefined")
Exception Type: NameError at /dashboard/video/external
Exception Value: Undefined