如何通过点符号

时间:2015-10-07 15:34:56

标签: python oop syntax pyramid

目标:从video_id拉出assessment_obj,分配给变量
数据库类型:SQLAlchemy
模型:Assessment持有video_id多对一关系(多个评估可以使用视频)

1。)我正在尝试使用dot.notation从评估中提取video_id。)以便我可以使用retrieve方法提取视频信息(例如url)。

目的:视频url将用于通过src模板/ JINJA2代码进入视频播放器HTML(如果我不正确地使用行话,我很抱歉,因为我是新手,并且我正在努力更好地理解这些概念。)

我试图关注OOP dot.notation tutorial,但不确定如何从表中提取数据并分配给变量而不会意外地重新分配变量。

views.py
表单视图配置。

@view_config(route_name='save_assessment_result', renderer='templates/completedpage.jinja2')
def save_assessment_result(request):
    with transaction.manager:
        assessment_id = int(request.params['assessment_id'])
        assessment_obj = api.retrieve_assessment(assessment_id) # the assessment

        assessment_obj.video_id = video # this right? 
        thevid = api.retrieve_video(video) #retrieves the video via ID
        template_video = int(request.params['video'])
        # how do I get the retrieved video 'thevid' into the template? 

        # more code 

       transaction.commit()
       return HTTPCreated()

模板: templates/assessment_form.jinja2

  <div class="embed-responsive embed-responsive-16by9">
    <iframe width="640" height="360" class="embed-responsive-item" allowfullscreen name='video' src="{{ url }}"></iframe>
  </div>

新:返回assessment_obj。新的View_Config。
注意: 'videoname': video.videoname, 'length': video.length, 'url': video.url

@view_config(route_name='assessment', request_method='GET', renderer='templates/assessment_form.jinja2')
def assessment_form_view(request):
    with transaction.manager:
        assessment_id = int(request.matchdict['id'])
        assessment = api.retrieve_assessment(assessment_id)
        if not assessment:
            raise HTTPNotFound()
        video_id = int(request.matchdict['id']) # <--- this gives the assessmnet_id and not the video
        video = api.retrieve_video(video_id)
        return {'assessment_id': assessment_id, 'assessment_name': assessment.name, 'assessment_text': assessment.text, 'user_id': assessment.user_id, 'video_id':assessment.video_id, 'categories': ','.join([str(i) for i in assessment.categories]), 'video_id':video_id, 'videoname': video.videoname, 'length': video.length, 'url': video.url}

2 个答案:

答案 0 :(得分:1)

assessment_obj.video_id = video行中 - video变量来自哪里?

看到你正在尝试&#34;从表中提取数据并分配给变量&#34;我想知道您是否真的尝试将assessment_obj.video_id分配给名为video的变量。在这种情况下,它应该是另一种方式:

video = assessment_obj.video_id

然后你的代码几乎开始有意义了。要将数据传递给模板,只需从视图函数中返回一个字典:

@view_config(route_name='save_assessment_result', renderer='templates/assessment_form.jinja2')
def save_assessment_result(request):
    with transaction.manager:
        assessment_id = int(request.params['assessment_id'])
        assessment_obj = api.retrieve_assessment(assessment_id) # the assessment

        video_id = assessment_obj.video_id
        thevid = api.retrieve_video(video_id)
        template_video = int(request.params['video'])
        return {
           'thevid': thevid,
           'template_video': template_video
        }

然后在您的模板中,您可以引用这些变量及其成员:

<iframe ... src="{{ thevid.url }}"></iframe>

答案 1 :(得分:0)

Per Sergey的惊人指导我创建了一个加载评估和视频的新视图配置。然后我返回了所需的值。

工作代码:

@view_config(route_name='assessment', request_method='GET', renderer='templates/assessment_form.jinja2')
def assessment_form_view(request):
    with transaction.manager:
        assessment_id = int(request.matchdict['id'])
        assessment = api.retrieve_assessment(assessment_id)
        if not assessment:
            raise HTTPNotFound()

        video = assessment.video_id
        print 'test_video_id', video
        video_obj = api.retrieve_video(video)
        print 'test_video_obj', video_obj

        return {'assessment_id': assessment_id, 'assessment_name': assessment.name, 'assessment_text': assessment.text, 'user_id': assessment.user_id, 'categories': ','.join([str(i) for i in assessment.categories]), 'url': video_obj.url, 'video_id':video_obj.video_id, 'videoname': video_obj.videoname, 'length': video_obj.length}