覆盖AllAuth视图并将上下文数据传递给模板



我不确定我的标题是否正确,但我有一个标准的base.html文件,其中包括来自被覆盖的所有身份验证模板的块内容,主要是account/email.html、account/password_change.html、account/papassword_set.html和socialacount/connections.html。根据选择的选项卡,每个模板都会输入base.html。选项卡对应于默认的allauth视图、accounts/email、accounts/password/change、accounts/password/set和accounts/social/connections。

当我加载base.html时,我能够从自定义配置文件模型中获取用户配置文件信息

@login_required
def base(request):
email = request.user
profile = Profile.objects.filter(my_user=email)
return render(request, 'base.html', {'profile': profile})

只要我在base.html模板视图中,用户名就会显示在屏幕的右上角,并且我可以访问模板中使用的所有配置文件信息。但是,当我单击allauth默认视图(该视图仍然扩展base.html(时,标头中的用户名信息将消失,配置文件数据将不再可用,直到我单击返回主base.html视图。

有人能帮我解决我缺少的问题吗?如何将屏幕右上角的用户名始终保持在base.html文件中(即使视图发生变化(,以及访问不同视图中的配置文件信息?

为了防止这有助于其他人覆盖AllAuth视图,可以通过:解决

class MyEmailView(EmailView):
def get_context_data(self, **kwargs):
email = self.request.user
profile_data = Profile.objects.filter(my_user=email)
context = super(MyEmailView, self).get_context_data(**kwargs)
context["profile_data"] = profile_data
print(context["profile_data"])
return context

urls.py

# override of email view to add user profile context data
path("accounts/email/", MyEmailView.as_view(), name="account_email"),

最新更新