django消息框架在打印时返回None



我是django的新手,我遇到了一个明显的问题,我无法展开,在我成功提交的表单上,在用户将被重定向到的主主页上放了一条消息,问题是它没有返回消息。参见下面视图:

def service(request):
form = GetStartedForm(request.POST or None)
if form.is_valid():
form.save()
x = messages.success(request, 'We have recorded your request and we will contact you soon!')
print(x)
print('Form Successfull!')  
return HttpResponseRedirect(reverse_lazy('home'))

context = {'form': form}
return render(request, 'bluetec/service.html', context)

形式:

class GetStartedForm(forms.ModelForm):
class Meta:
model = GetStarted
fields = '__all__'

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['name'].widget.attrs.update({'size': 5, 'class': 'form-control', 'placeholder': 'Enter your name:'})
self.fields['phone_no'].widget.attrs.update({'size': 5, 'title': 'Phone No', 'class': 'form-control', 'placeholder': 'Enter your phone no:'})
self.fields['email'].widget.attrs.update({'size': 10, 'title': 'Email', 'class': 'form-control', 'placeholder': 'Enter your email:'})
self.fields['description'].widget.attrs.update({'size': 10, 'title': 'description', 'class': 'form-control', 'placeholder': 'Enter your projects description:'})

html是

<body>
<div id="wrapper" >
{% include 'bluetec/messages.html' %}
<!-- header begin -->
<header class="{% block class %} header-light transparent scroll-light {% endblock class %}">
<div class="container">
<div class="row">

messages.html文件是

{% if messsages %}
{% for message in messages %}
<div class="alert alert-{{ message.tags }}" id="msg" role="alert">
{{ message }}
</div>
{% endfor %}
{% endif %}

当我打印x值以查看它返回的内容时,它返回的是"None"one_answers"form Successfull"我尝试了很多答案,但没有一个能说明我的问题。idk什么问题是任何帮助都将不胜感激,

#you can use in views.py like this 
form.save()
messages.success(request, 'Your message')
return HttpResponseRedirect(reverse_lazy('home'))
#and in html template like this
{% for msg in messages %}
<div class="alert {{msg.tags}} alert-dismissible fade show" role="alert">
<strong>{{msg}}</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
{% endfor %}
#tags will show type of message like success, error, warning...etc
{{msg.tags}}
#this will show the your custom text message sent from views
{{msg}} 
#i guess there is no need for this
{% if messsages %} #it should be messages
{% endif %}
#hope this works for you

以下是的问题

x = messages.success(request, 'We have recorded your request and we will contact you soon!')

只需这样写信息:

messages.success(request, 'We have recorded your request and we will contact you soon!')

您不必将消息分配给可变

问题是因为我没有使用cdn,而是使用付费模板,因为警报类没有被使用,所以我在所有导入的顶部导出了cdn,这样它就不会覆盖已经覆盖的样式,这解决了我的问题

最新更新