Last active
November 13, 2020 07:00
-
-
Save Shrestha7/0bf321797518815906e68265bcba954b to your computer and use it in GitHub Desktop.
Django crud with ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib import admin | |
from .models import Book | |
admin.site.register(Book) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% load static %} | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Bookstore</title> | |
<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet"> | |
</head> | |
<body> | |
<div class="container"> | |
{% block content %} | |
{% endblock %} | |
</div> | |
<script src="{% static 'js/jquery-3.1.1.min.js' %}"></script> | |
<script src="{% static 'js/bootstrap.min.js' %}"></script> | |
<script src="{% static 'js/plugin.js' %}"></script> | |
{% block javascript %} | |
{% endblock %} | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% load crispy_forms_tags %} | |
<form method="post" data-url="{% url 'book_create' %}" class="create-form"> | |
{% csrf_token %} | |
<div class="modal-header"> | |
<h5 class="modal-title" >Create Book</h5> | |
<button type="button" class="close" data-dismiss="modal" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
</div> | |
<div class="modal-body"> | |
{{form|crispy}} | |
</div> | |
<div class="modal-footer"> | |
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> | |
<button type="button" class="btn btn-primary">Save changes</button> | |
</div> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render | |
from .models import Book | |
from .forms import BookForm | |
from django.http import JsonResponse | |
from django.template.loader import render_to_string | |
def book_list(request): | |
books = Book.objects.all() | |
context = { | |
'books': books | |
} | |
return render(request, 'book_list.html',context) | |
def book_create(request): | |
data = dict() | |
if request.method == 'POST': | |
form = BookForm(request.POST) | |
if form.is_valid(): | |
form.save() | |
data['form_is_valid'] = True | |
books = Book.objects.all() | |
data['book_list'] = render_to_string('book_list_2.html',{'books':books}) | |
else: | |
data['form_is_valid'] = False | |
else: | |
form = BookForm() | |
context = { | |
'form':form | |
} | |
data['html_form'] = render_to_string('book_create.html',context,request=request) | |
return JsonResponse(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% load crispy_forms_tags %} | |
<form method="post" data-url="{% url 'book_delete' book.id %}" class="delete-form"> | |
{% csrf_token %} | |
<div class="modal-header"> | |
<h5 class="modal-title" >Delete Book</h5> | |
<button type="button" class="close" data-dismiss="modal" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
</div> | |
<div class="modal-body"> | |
<p class="lead"> Are you sure you want to delete this book <strong>{{book.title}}</strong></p> | |
</div> | |
<div class="modal-footer"> | |
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> | |
<button type="submit" class="btn btn-danger">Delete book</button> | |
</div> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'base.html' %} | |
{% block content %} | |
<h1 class="page-header">Books</h1> | |
<button class="btn btn-primary show-form"> | |
<span class="glyphicon glyphicon-plus"></span> | |
New Book | |
</button> | |
<table class="table" id="book-table"> | |
<thead> | |
<tr> | |
<th>#</th> | |
<th>Title</th> | |
<th>Author</th> | |
<th>Type</th> | |
<th>Publication date</th> | |
<th>Pages</th> | |
<th>Price</th> | |
</tr> | |
</thead> | |
<tbody> | |
{% for book in books %} | |
<tr> | |
<td>{{ book.id }}</td> | |
<td>{{ book.title }}</td> | |
<td>{{ book.author }}</td> | |
<td>{{ book.get_book_type_display }}</td> | |
<td>{{ book.publication_date }}</td> | |
<td>{{ book.pages }}</td> | |
<td>{{ book.price }}</td> | |
</tr> | |
{% empty %} | |
<tr> | |
<td colspan="7" class="text-center bg-warning">No book</td> | |
</tr> | |
{% endfor %} | |
</tbody> | |
</table> | |
<div class="modal fade" id="modal-book"> | |
<div class="modal-dialog"> | |
<div class="modal-content"></div> | |
</div> | |
</div> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% for book in books %} | |
<tr> | |
<td>{{ book.id }}</td> | |
<td>{{ book.title }}</td> | |
<td>{{ book.author }}</td> | |
<td>{{ book.get_book_type_display }}</td> | |
<td>{{ book.publication_date }}</td> | |
<td>{{ book.pages }}</td> | |
<td>{{ book.price }}</td> | |
</tr> | |
{% empty %} | |
<tr> | |
<td colspan="7" class="text-center bg-warning">No book</td> | |
</tr> | |
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% load crispy_forms_tags %} | |
<form method="post" data-url="{% url 'book_update' form.instance.id %}" class="update-form"> | |
{% csrf_token %} | |
<div class="modal-header"> | |
<h5 class="modal-title" >Update Book</h5> | |
<button type="button" class="close" data-dismiss="modal" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
</div> | |
<div class="modal-body"> | |
{{form|crispy}} | |
</div> | |
<div class="modal-footer"> | |
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> | |
<button type="submit" class="btn btn-primary">edit book</button> | |
</div> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'base.html' %} | |
{% block content %} | |
<h1 class="page-header">Books</h1> | |
<button class="btn btn-primary show-form"> | |
<span class="glyphicon glyphicon-plus"></span> | |
New Book | |
</button> | |
<table class="table" id="book-table"> | |
<thead> | |
<tr> | |
<th>#</th> | |
<th>Title</th> | |
<th>Author</th> | |
<th>Type</th> | |
<th>Publication date</th> | |
<th>Pages</th> | |
<th>Price</th> | |
</tr> | |
</thead> | |
<tbody> | |
{% include "book_list_2.html" %} | |
</tbody> | |
</table> | |
<div class="modal fade" id="modal-book"> | |
<div class="modal-dialog"> | |
<div class="modal-content"></div> | |
</div> | |
</div> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function(){ | |
$('.show-form').click(function(){ | |
$.ajax({ | |
url: '/books/create', | |
type: 'get', | |
dataType:'json', | |
beforeSend: function(){ | |
$('#modal-book').modal('show'); | |
}, | |
success: function(data){ | |
$('#modal-book .modal-content').html(data.html_form); | |
} | |
}); | |
}); | |
$('#modal-book').on('submit','.create-form' , function(){ | |
var form = $(this); | |
$.ajax({ | |
url: form.attr('data-url'), | |
data: form.serialize(), | |
type: form.attr('method'), | |
dataType: 'json', | |
success: function(data){ | |
if(data.form_is_valid){ | |
$('#book-table tbody').html(data.book_list); | |
$('#modal-book').modal('hide'); | |
} else { | |
$('#modal-book .modal-content').html(data.html_form) | |
} | |
} | |
}) | |
return false; | |
}) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render | |
from .models import Book | |
from .forms import BookForm | |
from django.http import JsonResponse | |
from django.template.loader import render_to_string | |
def book_list(request): | |
books = Book.objects.all() | |
context = { | |
'books': books | |
} | |
return render(request, 'book_list.html',context) | |
def book_create(request): | |
data = dict() | |
if request.method == 'POST': | |
form = BookForm(request.POST) | |
if form.is_valid(): | |
form.save() | |
data['form_is_valid'] = True | |
books = Book.objects.all() | |
data['book_list'] = render_to_string('book_list_2.html',{'books':books}) | |
else: | |
data['form_is_valid'] = False | |
else: | |
form = BookForm() | |
context = { | |
'form':form | |
} | |
data['html_form'] = render_to_string('book_create.html',context,request=request) | |
return JsonResponse(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% for book in books %} | |
<tr> | |
<td>{{ book.id }}</td> | |
<td>{{ book.title }}</td> | |
<td>{{ book.author }}</td> | |
<td>{{ book.book_type }}</td> | |
<td>{{ book.publication_date }}</td> | |
<td>{{ book.pages }}</td> | |
<td>{{ book.price }}</td> | |
<td> | |
<button class="btn btn-warning show-form-update" data-url="{% url 'book_update' book.id %}"> | |
<span class="glyphicon glyphicon-pencil"></span> | |
Edit | |
</button> | |
</td> | |
<td> | |
<button class="btn btn-danger show-form-delete" data-url="{% url 'book_delete' book.id %}"> | |
<span class="glyphicon glyphicon-trash"></span> | |
Delete | |
</button> | |
</td> | |
</tr> | |
{% empty %} | |
<tr> | |
<td colspan="7" class="text-center bg-warning">No book</td> | |
</tr> | |
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function(){ | |
var ShowForm = function(){ | |
var btn = $(this); | |
$.ajax({ | |
url: btn.attr("data-url"), | |
type: 'get', | |
dataType:'json', | |
beforeSend: function(){ | |
$('#modal-book').modal('show'); | |
}, | |
success: function(data){ | |
$('#modal-book .modal-content').html(data.html_form); | |
} | |
}); | |
}; | |
var SaveForm = function(){ | |
var form = $(this); | |
$.ajax({ | |
url: form.attr('data-url'), | |
data: form.serialize(), | |
type: form.attr('method'), | |
dataType: 'json', | |
success: function(data){ | |
if(data.form_is_valid){ | |
$('#book-table tbody').html(data.book_list); | |
$('#modal-book').modal('hide'); | |
} else { | |
$('#modal-book .modal-content').html(data.html_form) | |
} | |
} | |
}) | |
return false; | |
} | |
// create | |
$(".show-form").click(ShowForm); | |
$("#modal-book").on("submit",".create-form",SaveForm); | |
//update | |
$('#book-table').on("click",".show-form-update",ShowForm); | |
$('#modal-book').on("submit",".update-form",SaveForm) | |
//delete | |
$('#book-table').on("click",".show-form-delete",ShowForm); | |
$('#modal-book').on("submit",".delete-form",SaveForm) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf.urls import url, include | |
from . import views | |
urlpatterns = [ | |
url(r'^books/$', views.book_list, name='book_list'), | |
url(r'^books/create$', views.book_create, name='book_create'), | |
url(r'^books/(?P<id>\d+)/update$', views.book_update, name='book_update'), | |
url(r'^books/(?P<id>\d+)/delete$', views.book_delete, name='book_delete'), | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render,get_object_or_404 | |
from .models import Book | |
from .forms import BookForm | |
from django.http import JsonResponse | |
from django.template.loader import render_to_string | |
def book_list(request): | |
books = Book.objects.all() | |
context = { | |
'books': books | |
} | |
return render(request, 'book_list.html',context) | |
def save_all(request,form,template_name): | |
data = dict() | |
if request.method == 'POST': | |
if form.is_valid(): | |
form.save() | |
data['form_is_valid'] = True | |
books = Book.objects.all() | |
data['book_list'] = render_to_string('book_list_2.html',{'books':books}) | |
else: | |
data['form_is_valid'] = False | |
context = { | |
'form':form | |
} | |
data['html_form'] = render_to_string(template_name,context,request=request) | |
return JsonResponse(data) | |
def book_create(request): | |
if request.method == 'POST': | |
form = BookForm(request.POST) | |
else: | |
form = BookForm() | |
return save_all(request,form,'book_create.html') | |
def book_update(request,id): | |
book = get_object_or_404(Book,id=id) | |
if request.method == 'POST': | |
form = BookForm(request.POST,instance=book) | |
else: | |
form = BookForm(instance=book) | |
return save_all(request,form,'book_update.html') | |
def book_delete(request,id): | |
data = dict() | |
book = get_object_or_404(Book,id=id) | |
if request.method == "POST": | |
book.delete() | |
data['form_is_valid'] = True | |
books = Book.objects.all() | |
data['book_list'] = render_to_string('book_list_2.html',{'books':books}) | |
else: | |
context = {'book':book} | |
data['html_form'] = render_to_string('book_delete.html',context,request=request) | |
return JsonResponse(data) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from .models import Book | |
class BookForm(forms.ModelForm): | |
publication_date = forms.DateTimeInput() | |
class Meta: | |
model = Book | |
fields = ('title', 'publication_date', 'author', 'price', 'pages', 'book_type', ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class Book(models.Model): | |
BOOK_TYPES = ( | |
(u"Hardcover", u'Hardcover'), | |
(u"Paperback", u'Paperback'), | |
(u"E-book", u'E-book'), | |
) | |
title = models.CharField(max_length=50) | |
publication_date = models.DateField(null=True) | |
author = models.CharField(max_length=30, blank=True) | |
price = models.DecimalField(max_digits=5, decimal_places=2) | |
pages = models.IntegerField(blank=True, null=True) | |
book_type = models.CharField(choices=BOOK_TYPES,max_length=120) | |
def __str__(self): | |
return self.title |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function(){ | |
$('.show-form').click(function(){ | |
$.ajax({ | |
url: '/books/create', | |
type: 'get', | |
dataType:'json', | |
beforeSend: function(){ | |
$('#modal-book').modal('show'); | |
}, | |
success: function(data){ | |
$('#modal-book .modal-content').html(data.html_form); | |
} | |
}) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
django==1.10 | |
django-crispy-forms==1.6.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'base.html' %} | |
{% block content %} | |
<h1 class="page-header">Books</h1> | |
<button class="btn btn-primary show-form" data-url="{% url 'book_create' %}"> | |
<span class="glyphicon glyphicon-plus"></span> | |
New Book | |
</button> | |
<table class="table" id="book-table"> | |
<thead> | |
<tr> | |
<th>#</th> | |
<th>Title</th> | |
<th>Author</th> | |
<th>Type</th> | |
<th>Publication date</th> | |
<th>Pages</th> | |
<th>Price</th> | |
</tr> | |
</thead> | |
<tbody> | |
{% include "book_list_2.html" %} | |
</tbody> | |
</table> | |
<div class="modal fade" id="modal-book"> | |
<div class="modal-dialog"> | |
<div class="modal-content"></div> | |
</div> | |
</div> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% for book in books %} | |
<tr> | |
<td>{{ book.id }}</td> | |
<td>{{ book.title }}</td> | |
<td>{{ book.author }}</td> | |
<td>{{ book.get_book_type_display }}</td> | |
<td>{{ book.publication_date }}</td> | |
<td>{{ book.pages }}</td> | |
<td>{{ book.price }}</td> | |
<td> | |
<button class="btn btn-warning show-form-update" data-url="{% url 'book_update' book.id %}"> | |
<span class="glyphicon glyphicon-pencil"></span> | |
Edit | |
</button></td> | |
</tr> | |
{% empty %} | |
<tr> | |
<td colspan="7" class="text-center bg-warning">No book</td> | |
</tr> | |
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function(){ | |
var ShowForm = function(){ | |
var btn = $(this); | |
$.ajax({ | |
url: btn.attr("data-url"), | |
type: 'get', | |
dataType:'json', | |
beforeSend: function(){ | |
$('#modal-book').modal('show'); | |
}, | |
success: function(data){ | |
$('#modal-book .modal-content').html(data.html_form); | |
} | |
}); | |
}; | |
var SaveForm = function(){ | |
var form = $(this); | |
$.ajax({ | |
url: form.attr('data-url'), | |
data: form.serialize(), | |
type: form.attr('method'), | |
dataType: 'json', | |
success: function(data){ | |
if(data.form_is_valid){ | |
$('#book-table tbody').html(data.book_list); | |
$('#modal-book').modal('hide'); | |
} else { | |
$('#modal-book .modal-content').html(data.html_form) | |
} | |
} | |
}) | |
return false; | |
} | |
// create | |
$(".show-form").click(ShowForm); | |
$("#modal-book").on("submit",".create-form",SaveForm); | |
//update | |
$('#book-table').on("click",".show-form-update",ShowForm); | |
$('#modal-book').on("submit",".update-form",SaveForm) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf.urls import url, include | |
from . import views | |
urlpatterns = [ | |
url(r'^books/$', views.book_list, name='book_list'), | |
url(r'^books/create$', views.book_create, name='book_create'), | |
url(r'^books/(?P<id>\d+)/update$', views.book_update, name='book_update'), | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render,get_object_or_404 | |
from .models import Book | |
from .forms import BookForm | |
from django.http import JsonResponse | |
from django.template.loader import render_to_string | |
def book_list(request): | |
books = Book.objects.all() | |
context = { | |
'books': books | |
} | |
return render(request, 'book_list.html',context) | |
def save_all(request,form,template_name): | |
data = dict() | |
if request.method == 'POST': | |
if form.is_valid(): | |
form.save() | |
data['form_is_valid'] = True | |
books = Book.objects.all() | |
data['book_list'] = render_to_string('book_list_2.html',{'books':books}) | |
else: | |
data['form_is_valid'] = False | |
context = { | |
'form':form | |
} | |
data['html_form'] = render_to_string(template_name,context,request=request) | |
return JsonResponse(data) | |
def book_create(request): | |
if request.method == 'POST': | |
form = BookForm(request.POST) | |
else: | |
form = BookForm() | |
return save_all(request,form,'book_create.html') | |
def book_update(request,id): | |
book = get_object_or_404(Book,id=id) | |
if request.method == 'POST': | |
form = BookForm(request.POST,instance=book) | |
else: | |
form = BookForm(instance=book) | |
return save_all(request,form,'book_update.html') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf.urls import url, include | |
from . import views | |
urlpatterns = [ | |
url(r'^books/$', views.book_list, name='book_list'), | |
url(r'^books/create$', views.book_create, name='book_create'), | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render | |
from .models import Book | |
from .forms import BookForm | |
from django.http import JsonResponse | |
from django.template.loader import render_to_string | |
def book_list(request): | |
books = Book.objects.all() | |
context = { | |
'books': books | |
} | |
return render(request, 'book_list.html',context) | |
def book_create(request): | |
form = BookForm() | |
context = { | |
'form':form | |
} | |
html_form = render_to_string('book_create.html',context,request=request) | |
return JsonResponse({'html_form':html_form}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment