请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

NameError at /dashboard/video/external

老师好! 谢谢老师的课程,第一次可以坚持到现在。我成功创建了外链页面,但是不知道什么原因在添加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

正在回答 回答被采纳积分+3

2回答

deweizhang 2020-01-04 09:05:17

你看下你video.py 22 行 data里是否传了没定义的key name 因为你传错了(写错)所以报错了 ~

0 回复 有任何疑惑可以回复我~
提问者 买茶茶 2020-01-04 08:54:27

另外, 可以请教一下老师或者同学有项目的源代码吗,在git仓库里没有看到。希望可以对照一下。谢谢大家!

0 回复 有任何疑惑可以回复我~
  • 有源码 可以联系助教老师要地址~
    回复 有任何疑惑可以回复我~ 2020-01-04 09:05:45
  • 提问者 买茶茶 回复 deweizhang #2
    好的好的,谢谢老师!
    回复 有任何疑惑可以回复我~ 2020-01-04 09:09:35
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信