I have a problem, I have a video field and I want to save in the session if the user finished watching a video. I am planing on saving the video id (video is in my django db).
my code:
{% if True %} <!-- if user has acsses to this video/course-->
<div class="video_container">
<div>
<h2>{{lecture.name}}</h2>
</div>
<div>
<div>
<video id="video" controls controlsList="nodownload" poster="">
<source type="video/mp4" src="{{lecture.video.url}}"></source>
</video>
</div>
</div>
</div>
{% else %}
<p>אין לך גישה לפה יא מניאק</p>
{% endif %}
</div>
<script>
document.getElementById('video').addEventListener('ended', function(e) {
sessionStorage.setItem("watched_videos", "SessionValue");
});
</script>
and in a diffrent view i have this statment
{% if str(lecture.id) in request.session.watched_videos %} <!--if user watched the video-->
<i class="fas fa-play-circle"></i>
{% else %}
<i class="far fa-play-circle"></i>
{% endif %}
<i class="{{lecture.L_type.icon}}"></i>
<h2 class="lecture_name">{{lecture.name}}</h2>
my problems are -
a) watched_videos needs to be a list containing a lot the videos ids, not just one, for example [1,2,3,4,5…] and I want to insert into it not update it.
b) I don’t know if its possible but I want to keep the session information about watched_videos for 4 months but the user login for 2 weeks. is it possible?
another question:
my db is structure like so:
User → purchses → bundles → items
user - purchses is one to many. purchses - bundles is many to many and bundles - items is many to many
I want to get all the Items the user bough,
is there a better way than this
# get all bundles a user bought
a=[x.bundles.all() for x in user.purchses .all()]
b = []
#combine all the bundles to one list of bundles
for bundles in a:
for bundle in bundles
b.append bundle
#get the items from the bundles
items = []
for bundle in b:
for item in bundle.items.all()
items.append(item)
return item
Note: The ended event does not mean that the person watched the video. You have no way to know if they watched the video or just advanced the video to the end.
This generally implies a table related to your user table, where each row is a video id. (Possibly a many-to-many relationship between videos and users through a “watched video” model.)
There’s nothing within Django to automatically delete data after some period of time, but this can be done by identifying when you need to check this, and delete the old data at that time.
You can cross multiple relationships using the __ notation.
“This generally implies a table related to your user table, where each row is a video id. (Possibly a many-to-many relationship between videos and users through a “watched video” model.)”
isn’t it smarter to save the information locally on the session?
hey, im trying to save if a user finshed watching a video on my website, I wrote the following code and I dont understand why it doesnt work.
my idea is, on the page run a js lister, when the end of the video is triggered send a form to a url which updates db.
in the db the user class has a"watched videos" var in a many to many relasionship with lecture (each lecture contains a video).
when I ran it i got a 403 error, which is probably caused due to not having a csrf token, I tried some suggested stuff on stackoverflow but most of the suggestions are for form submissions, how can I add csrf token without a form?
every course have many sections, and every section has many lectures. I made a many to many relation ship.
when I present the data, I qurry the data, for example course.sections.all() and get back all the sections in a specific course.
when I present them I just make a for, the problem is the order of the sections, some needs to be before others.
my question:
what is the order that the items from back from the db? the order they were added to the many to many relation or ordered by id?
if the second one is the correct, is there any way to order them like I want? (not by adding a number to the section because the same section might be the first section in one course and the last one in another). thanks !
SQL query result sets are by definition, unordered. If you want the data returned in some specific order, you need to use an order_by clause to specify the sequence. It is wrong to rely upon the database to return data in order.