Skip to content

Instantly share code, notes, and snippets.

class PersonBase(models.Model):
name = models.Charfield()
address = models.Charfield()
class Meta:
abstract = True
class Employee(PersonBase):
emp_id = models.CharField()
@fxdgear
fxdgear / models.py
Created July 12, 2012 02:04
Simple Models
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
author = models.ForeignKey(Author)
title = models.CharField(max_length=100)
from django import forms
from django.forms.models import inlineformset_factory
from .models import Author, Book
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
from django.views.generic import CreateView
from django.shortcuts import redirect
from .models import Author
from .forms import AuthorForm, BookFormSet
class AddAuthorView(CreateView):
template_name = 'create_author.html'
form_class = AuthorForm
<h1>Add an Author and Books</h1>
<form class="form-horizontal form-inline" method="post" >
{% csrf_token %}
<legend>Author Details</legend>
{{ form }}
{{ formset.management_form }}
<legend>Books</legend>
<div class="books">
{% for book_form in formset %}
<script src="{{ STATIC_URL }}/js/jquery.js" ></script>
<script src="{{ STATIC_URL }}/js/underscore.js" ></script>
<script>
$('.add-book').click(function(ev){
ev.preventDefault();
var count = $('.books').children().length;
var tmplMarkup = $('#book-template').html();
var compiledTmpl = _.template(tmplMarkup, { id : count });
$('div.books').append(compiledTmpl);
<script type="text/html" id="book-template">
<div id="book-<%= id %>">
<label for="id_books-<%= id %>-title">Book Title:</label>
<input id="id_books-<%= id %>-title" type="text" name="books-<%= id %>-title" maxlength="100">
<input type="hidden" name="books-<%= id %>-author" id="id_books-<%= id %>-author">
<input type="hidden" name="books-<%= id %>-id" id="id_books-<%= id %>-id">
</div>
</script>
$('.add-book').click(function(ev){
ev.preventDefault();
var count = $('.books').children().length;
var tmplMarkup = $('#book-template').html();
var compiledTmpl = _.template(tmplMarkup, { id : count });
$('div.books').append(compiledTmpl);
// update form count
$('#id_books-TOTAL_FORMS').attr('value', count+1);
});
@fxdgear
fxdgear / gist:3146540
Created July 19, 2012 20:26
Template Choices
Something along these lines:
class SiteTemplate
name - Business user "friendly" name
path - The path to the actual template
type - ('style', 'boutique')
class SiteTemplateContext
"""
ERT WERS THE BERST ERF TERMAHS, ERT WERS THE WERST ERF TERMAHS, ERT WERS THE ERGE ERF WERSDERM, ERT WERS THE ERGE ERF FERLERSHNERSS, ERT WERS THE ERPERCH ERF BERLERF, ERT WERS THE ERPERCH ERF ERNCRERDERLERTER, ERT WERS THE SERSERN ERF LERGHT, ERT WERS THE SERSERN ERF DERKNERSS, ERT WERS THE SPRERNG ERF HERPER, ERT WERS THE WERNTER ERF DERSPER, WE HERD ERVERERTHERNG BERFERE ERS, WE HERD NERTHERNG BERFERE ERS, WE WERE ERLL GERNG DERERCT TO HERVERN, WE WERE ERLL GERNG DERERCT THE ERTHER WE - ERN SHERT, THE PERERD WERS SO FER LERKE THE PRERSERNT PERERD, THERT SERME ERF ERTS NERSERST ERTHERERTERS ERNSERSTERD ERN ERTS BERNG RERCERVERD, FER GERD ER FER ERVERL, ERN THE SERPERLERTERVE DERGRE ERF CERMPERERSERN ERNLER.