Skip to content

Instantly share code, notes, and snippets.

$('.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);
});
<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>
<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);
<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 %}
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
from django import forms
from django.forms.models import inlineformset_factory
from .models import Author, Book
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
@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)
class PersonBase(models.Model):
name = models.Charfield()
address = models.Charfield()
class Meta:
abstract = True
class Employee(PersonBase):
emp_id = models.CharField()
A guy walks into a bar and orders a shot of whisky. He gulps it down and peeks into his shirt pocket.
He then orders another shot of whisky, gulps it down and peeks into his short pocket.
He orders a third shot and does the same thing. After the sixth shot, he asks the bartender for the bill, pays and starts to walk out.
Curiosity gets the better of the bartender and he says to the guy:
"Excuse me, but I noticed that every time you drank a shot, you kept looking into your pocket. I was wondering what's in your pocket.
"The guy slurs, "Well, I have a picture of my wife in my pocket. I keep drinking until she starts to look good."
[program:program_name1]
environment = PYTHONPATH="<< set up your python path here >>",DJANGO_SETTINGS_MODULE="program.settings"
command=<< path to project >>/manage.py celeryd -v 2 -B -s celery -E -l INFO
user=nobody
numprocs=1
stdout_logfile=/var/log/celery/program_name1.log
stderr_logfile=/var/log/celery/program_name1.err
autostart=true
autorestart=true