Django, в шаблоне HTML я использовал jinja2 в id = snackbar, но когда я нажимаю на кнопку, всплывающее окно просто отображается, но в нем нет {{MSG}}, вот URL, представления для контакта. html:
URL
url(r'^contact', TemplateView.as_view(template_name= 'contact.html')),
url(r'^insertenquiry', insertenquiry, name='insertenquiry'),
просмотров
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})
форм
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)
моделей
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 удалены только сейчас.
<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);}