【发布时间】:2026-01-20 16:30:01
【问题描述】:
我正在尝试使用 Toastr 在完成表单时传递状态消息。如果表单有效,它会使用“返回索引(请求)”将您返回到索引,我想通过它传递上下文。
我正在使用 Modelforms 运行 Python 3.6 和 Django 1.11。我尝试过传递 context=var_list 以及单独传递变量。
我的views.py的第15-34行
def add(request):
form = CustomerForm()
if request.method == "POST":
form = CustomerForm(request.POST)
if form.is_valid():
form.save(commit=True)
notification_vars = {"toastr_title": "SUCCESS!",
"toastr_message": "The Customer has been added to the database",
"toastr_type": "success",}
return index(request, context=notification_vars) // THIS IS WHAT I NEED TO WORK
page_vars = { "page_title": "Add Customer",
"page_icon": "fa-address-book",
"toastr_title": "",
"toastr_message": "",
"toastr_type": "",
"form": form}
return render(request, 'customers/add.html', context=page_vars)
这是我页脚中的烤面包机代码,以及为什么除非我想传递消息,否则我总是必须将变量传递为空白
{% if toastr_message %}
<script>
var title = '{{ toastr_title }}',
message = '{{ toastr_message }}',
type = '{{ toastr_type }}',
options = {};
toastr[type](message, title, options);
</script>
{% endif %}
我想要做的是成功提交表单,它将上下文传递回索引页面并将脚本填充到页脚中,以便弹出成功消息。
目前,它回来告诉我我有一个意外的变量上下文,或者如果我直接传递变量,它告诉我有一个语法问题
【问题讨论】: