Django message framework with bootstrap alert
1.setting a import koro
from django.contrib.messages import constants as messages
NICHE LIKHO
MESSAGE_TAGS = {
messages.ERROR:'danger',
}
AIGULA KOERLAM CLASS AR NAME MELANOR JONNO BOOTSTRAP ER SATHE
view
from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from .forms import Registry1
from .models import Registry
from django.contrib import messages
# Create your views here.
def message(request):
if request.method=="POST":
obj=Registry1(request.POST)
if obj.is_valid():
name=obj.cleaned_data['name']
email=obj.cleaned_data['email']
password=obj.cleaned_data['password']
date=obj.cleaned_data['date']
sq=Registry(name=name,email=email,password=password,date=date)
sq.save()
obj=Registry1()
messages.success(request,'Form submission done2')
messages.error (request,'Form submission done3')
else:
messages.error (request,'thik kore submit kor')
return HttpResponseRedirect('/home/message/')
else:
obj=Registry1()
return render(request,'reg/message.html',{'form':obj})
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<!-- MESSEGE SHOWING PART -->
{%if messages %}
{% for i in messages %}
{% if i.tags %}
<div class="alert alert-{{i.tags}} alert-dismissible fade show" role="alert">
<strong>Success</strong> {{i}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{% endif %}
{%endfor%}
{% endif %}
<!-- FORMS PART -->
<form action="" method="POST">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="submit">
</form>
</body>
</html>
Comments
Post a Comment