我通过传递extra_tags在views.py中尝试了此方法,但对我而言不起作用。请建议为此提出另一种方法
views.py
class CustomPasswordResetConfirmView(SuccessMessageMixin, PasswordResetConfirmView):
success_message = "Your password has been set. You may go ahead and log in now. "
extra_tags = 'alert-success'
def get_success_message(self, cleaned_data):
return (self.success_message, self.extra_tags)
login.html
{% if messages %}
{% for message in messages %}
{% if message.extra_tags == 'alert-success' %}
<!-- alert-warning-green -->
<div class="alert-bx alert-warning-green mt-3">
<i class="icon-checked b-6 alert-icon mr-2"></i> {{message}}
</div>
<!-- alert-warning-green END -->
{% endif %}
{% if message.extra_tags == 'alert-danger' %}
<!-- error-alert -->
<div class="alert-bx error-alert mt-3">
<i class="icon-warning-triangle alert-icon mr-2"></i> {{message}}
</div>
<!-- error-alert END -->
{% endif %}
{% endfor %}
{% endif %}
答案 0 :(得分:1)
get_success_message
返回字符串消息,不支持extra_tags
。
为了支持它,您必须在form_valid
中修改SuccessMessageMixin
函数。
定义了默认功能:
def form_valid(self, form):
response = super().form_valid(form)
success_message = self.get_success_message(form.cleaned_data)
if success_message:
messages.success(self.request, success_message)
return response
要支持您的extra_tags
,您必须更改为此:
def form_valid(self, form):
response = super().form_valid(form)
success_message = self.get_success_message(form.cleaned_data)
if success_message:
messages.success(self.request, success_message, extra_tags=self.extra_tags)
return response
并删除修改后的get_success_message
答案 1 :(得分:0)
class CustomPasswordResetConfirmView(SuccessMessageMixin, PasswordResetConfirmView):
success_message = "Your password has been set. You may go ahead and log in now. "
def get_success_message(self, cleaned_data):
return messages.success(request,self.success_message, extra_tags = 'alert-success')