I am new to django, i have stuck into an obvious issue which i cant unfold, at the form successful submission i have, put a message on the main home page where the user will be redirect to, the issue is its not returning the message.see below view:
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)
form:
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:'})
and the html is
<body>
<div id="wrapper" >
{% include 'bluetec/messages.html' %}
<!-- header begin -->
<header >
<div >
<div >
the messages.html file is
{% if messsages %}
{% for message in messages %}
<div id="msg" role="alert">
{{ message }}
</div>
{% endfor %}
{% endif %}
when i print the x value to see whats its returning, its returning 'None' and 'form Successfull' i tried many answers not none points to my issue. idk what the issue is any help would be appreciated,
CodePudding user response:
#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 role="alert">
<strong>{{msg}}</strong>
<button type="button" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</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
CodePudding user response:
Here is the problem
x = messages.success(request, 'We have recorded your request and we will contact you soon!')
Just put the message like so:
messages.success(request, 'We have recorded your request and we will contact you soon!')
You don't have to assign the message to a variable
CodePudding user response:
the issue was because i didn't use cdn and was using a paid template, in that the alert class was not being used, so i exported cdn at the top of all import so that it wont override already overridden styling, that solved my issue