Try this soluction
<form method="POST" action="/insertenquiry/">
{% csrf_token %}
<!-- <label for="fname">First Name</label> -->
<input type="text" name="firstname" class="form-control" placeholder="Your firstname" required>
<!-- <label for="lname">Last Name</label> -->
<input type="text" name="lastname" class="form-control" placeholder="Your lastname" required>
<!-- <label for="email">Email</label> -->
<input type="text" name="email" class="form-control" placeholder="Your email address" required>
<!-- <label for="message">Message</label> -->
<textarea name="saysomething" cols="30" rows="10" class="form-control" placeholder="Say something about us" required></textarea>
<!-- Submit button -->
<input type="submit" value="SEND ENQUIRY">
<!-- The actual snackbar -->
<div id="snackbar">{{ MSG }}</div>
</form>
{% if MSG is defined %}
<!-- Code only appears if MSG exists -->
<script type="text/text/javascript">
// Get the snackbar DIV
var x = document.getElementById("snackbar");
// Add the "show" class to DIV
x.className = "show";
// After 3 seconds, remove the show class from DIV
setTimeout(function(){ x.className = x.className.replace("show", ""); }, 5000);
</script>
{% endif %}
Django, in HTML template i used jinja2 in id = snackbar but when i click on the button the pop is just showing but no {{MSG}} in it, here is the url, views for contact.html:
urls
url(r'^contact', TemplateView.as_view(template_name= 'contact.html')),
url(r'^insertenquiry', insertenquiry, name='insertenquiry'),
views
def insertenquiry(request):
if request.method == 'POST':
fn = request.POST['firstname']
ln = request.POST['lastname']
emailid = request.POST['email']
say = request.POST['saysomething']
obj = Enquiry(fname=fn, lname=ln, email=emailid, saysomething=say)
obj.save()
msg = "sent success"
return render(request,"contact.html",{'MSG':msg})
forms
from django import forms
from django.core import validators
class Enquiry(forms.Form):
firstname = forms.CharField(max_length=100)
lastname = forms.CharField(max_length=100)
email = forms.EmailField(max_length=100)
saysomething = forms.CharField(max_length=500)
models
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Enquiry(models.Model):
fname = models.CharField(max_length= 100,unique=True)
lname = models.CharField(max_length= 100)
email = models.EmailField(max_length=100,unique=True)
saysomething = models.CharField(max_length=500)
template: div tags are removed just for now.
<form method="POST" action="/insertenquiry/">{% csrf_token %}
<!-- <label for="fname">First Name</label> -->
<input type="text" name="firstname" class="form-control" placeholder="Your firstname" required>
<!-- <label for="lname">Last Name</label> -->
<input type="text" name="lastname" class="form-control" placeholder="Your lastname" required>
<!-- <label for="email">Email</label> -->
<input type="text" name="email" class="form-control" placeholder="Your email address" required>
<!-- <label for="message">Message</label> -->
<textarea name="saysomething" cols="30" rows="10" class="form-control" placeholder="Say something about us" required></textarea>
<!-- Use a button to open the snackbar -->
<button onclick="myFunction()">SEND ENQUIRY</button>
<!-- The actual snackbar -->
<div id="snackbar">{{MSG}}</div>
javascript
function myFunction() {
// Get the snackbar DIV
var x = document.getElementById("snackbar");
// Add the "show" class to DIV
x.className = "show";
// After 3 seconds, remove the show class from DIV
setTimeout(function(){ x.className = x.className.replace("show", ""); },
5000);}
I mean an HTML form.