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): | |
name = models.CharField(max_length=255) | |
isbn_number = models.CharField(max_length=13) | |
class Meta: | |
db_table = '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
from django import forms | |
class BookForm(forms.Form): | |
name = forms.CharField( | |
label='Book Name', | |
widget=forms.TextInput(attrs={ | |
'class': 'form-control', | |
'placeholder': 'Enter Book Name here' | |
}) | |
) |
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
# forms.py :: part 1 | |
from django import forms | |
from django.forms import formset_factory | |
class BookForm(forms.Form): | |
name = forms.CharField( | |
label='Book Name', | |
widget=forms.TextInput(attrs={ | |
'class': 'form-control', |
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
# views.py :: part 2 | |
from django.shortcuts import render, redirect | |
from .forms import BookFormset | |
from .models import Book | |
def create_book_normal(request): | |
template_name = 'store/create_normal.html' | |
heading_message = 'Formset Demo' |
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
<!-- create_normal.html :: part 3 --> | |
<form class="form-horizontal" method="POST" action=""> | |
{% csrf_token %} | |
{{ formset.management_form }} | |
{% for form in formset %} | |
<div class="row form-row spacer"> | |
<div class="col-2"> | |
<label>{{form.name.label}}</label> | |
</div> |
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
<!-- create_normal.html :: part 4 --> | |
<script type='text/javascript'> | |
function updateElementIndex(el, prefix, ndx) { | |
var id_regex = new RegExp('(' + prefix + '-\\d+)'); | |
var replacement = prefix + '-' + ndx; | |
if ($(el).attr("for")) $(el).attr("for", $(el).attr("for").replace(id_regex, replacement)); | |
if (el.id) el.id = el.id.replace(id_regex, replacement); | |
if (el.name) el.name = el.name.replace(id_regex, replacement); | |
} |
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
# forms.py :: part 1 | |
from django.forms import modelformset_factory | |
BookModelFormset = modelformset_factory( | |
Book, | |
fields=('name', ), | |
extra=1, | |
widgets={'name': forms.TextInput(attrs={ | |
'class': 'form-control', |
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
# views.py :: part 2 | |
from django.shortcuts import render, redirect | |
from .forms import BookModelFormset | |
def create_book_model_form(request): | |
template_name = 'store/create_normal.html' | |
heading_message = 'Model Formset Demo' | |
if request.method == 'GET': |
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
# forms.py :: part 1 | |
from django import forms | |
from django.forms import modelformset_factory | |
from .models import Book, Author | |
class BookModelForm(forms.ModelForm): | |
class Meta: | |
model = 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
# views.py :: part 2 | |
def create_book_with_authors(request): | |
template_name = 'store/create_with_author.html' | |
if request.method == 'GET': | |
bookform = BookModelForm(request.GET or None) | |
formset = AuthorFormset(queryset=Author.objects.none()) | |
elif request.method == 'POST': | |
bookform = BookModelForm(request.POST) | |
formset = AuthorFormset(request.POST) |
OlderNewer