1
我有試圖從我的Flask應用程序中的數據庫中拉出一個「隨機」項目的問題。此功能只需要返回用戶最近未觀看的視頻。我現在不擔心多個用戶。我目前的做法不起作用。這是我在用的:如何從列表中選擇隨機但不近期的資產?
@app.route('/_new_video')
def new_video():
這裏是我問的重要組成部分:
current_id = request.args.get('current_id')
video_id = random.choice(models.Video.query.all()) # return list of all video ids
while True:
if video_id != current_id:
new_video = models.Video.query.get(video_id)
,然後我返回它:
webm = new_video.get_webm() #returns filepath in a string
mp4 = new_video.get_mp4() #returns filepath in a string
return jsonify(webm=webm,mp4=mp4,video_id=video_id)
隨機範圍開始爲1,因爲第一個資產已從數據庫中刪除,所以數字0不與id關聯。理想情況下,用戶不會收到他們最近觀看的視頻。
此功能被放置在一個單獨的文件從我的路線和進口。然後,我在程序的開頭初始化了生成器,並將其發送給我的路由功能。 像這樣: [隨機數發生器(https://github.com/djds23/deanstream/blob/master/app/randomizer.py) 轉到: [路由](https://github.com/ djds23/deanstream/blob/master/app/routes.py) 非常感謝Aaron! – djds23