覆盖 AllAuth 视图并将上下文数据传递给模板
Override AllAuth Views and Pass context data to templates
我不确定我的标题是否正确,但是我有一个标准 base.html 文件,其中包含来自覆盖的 allauth 模板的块内容,主要是 account/email.html,account/password_change.html、account/password_set.html 和 socialaccount/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,header 中的用户名信息消失并且配置文件数据不再可用,直到我点击返回主 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"),
我不确定我的标题是否正确,但是我有一个标准 base.html 文件,其中包含来自覆盖的 allauth 模板的块内容,主要是 account/email.html,account/password_change.html、account/password_set.html 和 socialaccount/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,header 中的用户名信息消失并且配置文件数据不再可用,直到我点击返回主 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"),