0

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 class="{% block class %} header-light transparent scroll-light {% endblock class %}">
                <div class="container">
                    <div class="row">

the messages.html file is

{% if messsages %}
    {% for message in messages %}
        <div class="alert alert-{{ message.tags }}" 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,

Muhammad huzaifa
  • 144
  • 2
  • 10

3 Answers3

0
#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
  • :'( no, didnt work, i passed it without the declaration of variable x and it doesnt helped, and when i print the x, it says none, like the message is not created, dunno why – Muhammad huzaifa Apr 01 '22 at 06:21
  • Hello @Muhammadhuzaifa read about django [messages](https://docs.djangoproject.com/en/4.0/ref/contrib/messages/#module-django.contrib.messages). Django messages did not return any value it is used to display messages that you pass with request. – Ankit Tiwari Apr 01 '22 at 09:37
  • yes the issue was because i didnt use cdn was using a paid template, in that the alert class was not being used, that solved my issue – Muhammad huzaifa Apr 01 '22 at 11:48
0

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

Adil Mohak
  • 240
  • 2
  • 11
  • i did that to check the value of the messege, and its returning none and this did not helped :( – Muhammad huzaifa Apr 01 '22 at 06:20
  • make sure `'django.contrib.messages',` is in your INSTALLED_APPS, and `'django.contrib.messages.middleware.MessageMiddleware',` in MIDDLEWARE, and `'django.contrib.messages.context_processors.messages',` inside `TEMPLATES -> context_processors` – Adil Mohak Apr 01 '22 at 06:38
0

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

Muhammad huzaifa
  • 144
  • 2
  • 10