gpt4 book ai didi

javascript - 仅显示首次访问 Django 的介绍视频

转载 作者:行者123 更新时间:2023-12-03 00:41:09 24 4
gpt4 key购买 nike

我有一个 Django 应用程序,需要显示一个视频,该视频可以进入网站。我只希望它在初次访问时执行此操作,而不是每次用户刷新时执行此操作。我觉得 session 与此有关,但我不确定。谢谢!

最佳答案

我认为最好将此标志直接放入您的数据库中。您可以将字段放入用户模型中(如果您使用自定义用户)或与 User 具有 OneToOne 关系的模型中。例如:

class Profile(models.Model):
user = models.OneToOneField(User)
has_seen_intro = models.BooleanField(default=False)

并从 View 中将此信息发送到模板,如下所示:

class HomeView(TemplateView):
template_name = 'home.html'

def get_context_data(self, **kwargs):
context = super(HomeView, self).get_context_data(**kwargs)
profile = self.request.user.profile
if not profile.has_seen_intro:
context['show_intro'] = True
profile.has_seen_intro = False
profile.save()
# or use user.has_seen_intro if you have custom model
return context

并像这样更新模板

{% if show_intro %}
// intro video codes
{% endif %}

更新

对于匿名用户,请尝试这样:

class HomeView(TemplateView):
template_name = 'home.html'

def get_context_data(self, **kwargs):
context = super(HomeView, self).get_context_data(**kwargs)
if self.request.user.is_authenticated:
profile = self.request.user.profile
if not profile.has_seen_intro:
context['show_intro'] = True
profile.has_seen_intro = False
profile.save()
else:
if not self.request.session.get('has_seen_intro', True):
self.request.session['has_seen_intro'] = False
context['show_intro'] = True
return context

关于javascript - 仅显示首次访问 Django 的介绍视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53457103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com