Skip to content

Instantly share code, notes, and snippets.

View taranjeet's full-sized avatar

Taranjeet Singh taranjeet

View GitHub Profile
@taranjeet
taranjeet / models.py
Created June 12, 2018 05:51
Django Library : Book Model
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'
@taranjeet
taranjeet / forms.py
Last active June 12, 2018 05:54
Django Library: Book Normal Form
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'
})
)
@taranjeet
taranjeet / forms.py
Created June 12, 2018 05:55
Django Library: Book Formset
# 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',
@taranjeet
taranjeet / views.py
Created June 12, 2018 05:57
Django Library: Create Book View
# 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'
@taranjeet
taranjeet / create_normal.html
Last active June 12, 2018 09:13
Django Library: Normal Form html
<!-- 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>
@taranjeet
taranjeet / create_normal.html
Last active May 11, 2020 15:02
Django Library: Normal form js code
<!-- 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);
}
@taranjeet
taranjeet / forms.py
Created June 12, 2018 06:00
Django Library: Book Model Formset
# 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',
@taranjeet
taranjeet / views.py
Created June 12, 2018 06:01
Django Library: Model Formset view code
# 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':
@taranjeet
taranjeet / forms.py
Created June 12, 2018 06:03
Django Library: Author Formset
# 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
@taranjeet
taranjeet / views.py
Last active June 12, 2018 06:04
Django Library: Create Book with Author view
# 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)