Created
November 13, 2020 07:12
-
-
Save Shrestha7/ea71613222c1b2ff051644c4188fac09 to your computer and use it in GitHub Desktop.
Django crud with ajax create part 2
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="submit" class="btn btn-primary">add 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
{% 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
$(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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment