Goal: Pull video_id
from assessment_obj
, assign to variable
DB Type: SQLAlchemy
Model: Assessment
holds video_id
in a Many-to-One relationship (multiple assessments can use a video)
1.) I am trying to use dot.notation
to pull the video_id
from the assessment 2.) so that I can use the retrieve
method to pull the video information (such as url
).
Purpose: The video url
will be used to enter into a video player src
via a JINJA2
template/HTML
code. (I apologize if I am using jargon incorrectly as I am new and am trying to better understand the concepts.)
I was trying to follow a OOP
dot.notation
tutorial, but was uncertain on how to pull the data from a table and assign to a variable without accidently reassigning the variable.
views.py
The form view config.
@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()
template: 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>
NEW: Return for assessment_obj
. New View_Config.
Note: '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}
In the line assessment_obj.video_id = video
- where does the video
variable come from?
Seeing that you're trying "pull the data from a table and assign to a variable" I'm wondering if you actually tried to assign assessment_obj.video_id
to a variable called video
. In this case it should be another way round:
video = assessment_obj.video_id
Then you code almost starts to make sense. To pass the data to the template you simply return a dictionary from your view function:
@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
}
Then in your template you can reference those variables and their members:
<iframe ... src="{{ thevid.url }}"></iframe>