Skip to content

Instantly share code, notes, and snippets.

MIDDLEWARE_CLASSES = (
...
'app.middleware.exceptions.ExceptionMiddleware',
from django.shortcuts import _get_queryset
from app.exceptions.custom import NotFound
def get_object_or_template(klass, template, *args, **kwargs):
# replacement for django.shortcuts.get_object_or_404()
# allows a template to be supplied instead of a 404
"""
Uses get() to return an object, or raises a Http404 exception if the object
# get the product or show the not found template
product = get_object_or_template(Product, 'templates/product/not_found.html', pk=1)
class UserView(DetailView):
template_name = 'template.html'
#model = User
#context_object_name = 'foo'
def get_object(self):
return get_object_or_404(User, pk=request.session['user_id'])
arg_name = 'myarg'
some_method(**{arg_name: 'value'})
from django.views.generic import UpdateView
from django.shortcuts import get_object_or_404
from django.core.urlresolvers import reverse_lazy
from app.models import Model
from app.forms import Form1, Form2
class MyView(UpdateView):
template_name = 'template.html'
<form action="{% url 'myview' %}" method="post" enctype="multipart/form-data">
...
<input type="submit" name="form" value="Submit" />
</form>
<form action="{% url 'myview' %}" method="post" enctype="multipart/form-data">
...
<input type="submit" name="form2" value="Submit" />
</form>
return self.render_to_response(self.get_context_data(form=form))
# https://developer.apps.yahoo.com/projects
YAHOO_CONSUMER_KEY = 'YOUR-APP-ID'
YAHOO_CONSUMER_SECRET = 'YOUR-APP-SECRET'