djangosnippets.org: Latest snippets tagged with 'views'https://djangosnippets.org/tags/views/2016-01-06T19:37:09.149179-06:00delete object from table in form
2016-01-06T19:37:09.149179-06:00beckastarhttps://djangosnippets.org/snippets/10548/<p>I had a difficult time understanding how to delete an item from a table within a template, using a modelform. I couldn't find a good example, so I wanted to post the code that ultimately worked. </p>
Freely redistributableController Class for Views
2013-04-18T09:11:07.083032-05:00jovialbardhttps://djangosnippets.org/snippets/2911/<p>I wanted to be able to share common code among a subset of views without copy-and-pasting the code or the same function into each view, so I decided to wrap a class around the views so that the common code (i.e. loading a model that each of the views would …</p>
Freely redistributableHeader view decorators
2013-02-19T05:55:54.198438-06:00ydmhttps://djangosnippets.org/snippets/2895/<p>This file includes two Django view decorators <code>header</code> and <code>headers</code> that provide an easy way to set response headers.</p>
<p>Also, because I have to work with a lot of cross domain requests, I include few shortcuts for convenience to set the Access-Control-Allow-Origin header appropriately.</p>
Freely redistributableAlternative to Class Based Views
2012-09-12T11:52:13.136167-05:00sleepycalhttps://djangosnippets.org/snippets/2814/<p>There is a lot of debate on whether there is a real future for the Django CBVs (class based views). Personally, I find them tedious, and just wanted a way to keep my views clean.</p>
<p>So, here is a really minimalistic way of having class based views, without the fuss. …</p>
Freely redistributableEasier and Faster than flatpages. Rendering templates by simpling calling by his path on the URL
2012-02-29T05:25:45.908656-06:00mariocesarhttps://djangosnippets.org/snippets/2698/<p>This is in my opinion a better way to have flat pages in a project. In the example with the url patterns settings:</p>
<pre><code>/ will render -> /pages/welcome.html
/contact will render -> /pages/contact.html
/products/ will render -> /pages/products/index.html
/products/pricing will render -> /pages/products/pricing.html
</code></pre>
Freely redistributableRegister django views
2012-02-05T11:40:10.949906-06:00magik_cypresshttps://djangosnippets.org/snippets/2674/<p>Register django views</p>
Freely redistributableCache view by user (and anonymous)
2011-08-24T14:40:08.859250-05:00rafaelsdmhttps://djangosnippets.org/snippets/2524/<p>Use this decorator in your views to cache HttpResponse per user, so each user has his own cache, instead of a shared one as <code>from django.views.decorators.cache.cache_page</code> does.</p>
<p>Add this to use:
from somewhere import cache_per_user</p>
<p>@cache_per_user(ttl=3600, cache_post=False)
def my_view(request):
return HttpResponse("LOL %s"%(request.user))</p>
<p>All documentation inside the decorator are in brazilian …</p>
Freely redistributableClass that converts object to view.
2011-05-30T06:48:55.952636-05:00Pickelshttps://djangosnippets.org/snippets/2448/<p>Class that takes a normal none derived class and converts it into a view. The methods return simple datastructures which makes it easier to test.</p>
Freely redistributableAllow any view (probably a generic view) to accept captured URL variables into extra_context.
2010-10-28T15:28:54.432681-05:00orblivionhttps://djangosnippets.org/snippets/2245/<p>If your URL pattern looks like:</p>
<p><code>urlpatterns = patterns('django.views.generic.create_update',
url(r'^obj/(?P<obj_id>\d+)/new_thing$', create_object, {'form_class': ThingForm, 'template_name': 'thing/new_thing.html', extra_context: {:this":"that"}),
)</code></p>
<p>You will receive an error, because the create_update view doesn't have a parameter called "obj_id". Supposing you want that variable in the URL, and furthermore let's say you wanted to do something …</p>
Freely redistributablesimple_view
2010-06-30T16:07:44.227550-05:00trooleehttps://djangosnippets.org/snippets/2085/<p>Simplify views declararation. (Upd: add assertion)</p>
Freely redistributableView decorator providing confirmation dialog
2010-02-12T17:11:03.869897-06:00rudyrykhttps://djangosnippets.org/snippets/1922/<p>Decorator for views that need confirmation page. For example, delete
object view. Decorated view renders confirmation page defined by template
'template_name'. If request.POST contains confirmation key, defined
by 'key' parameter, then original view is executed.</p>
<p>Context for confirmation page is created by function 'context_creator',
which accepts same arguments as decorated …</p>
Freely redistributableDeclaring django views like web.py views
2010-02-05T14:22:17.415550-06:00danigmhttps://djangosnippets.org/snippets/1912/<p>I work a little with <a href="http://webpy.org/">web.py framework</a> and I like a lot the view definition.
</p>
<p>For each view you define a class and in that class you can define two method, GET and POST. If the http request is a GET request the GET method will be called and if …</p>
Freely redistributableFieldsets for Views
2010-01-22T10:45:48.936419-06:00Nadhttps://djangosnippets.org/snippets/1883/<p>This Snippet allows a view to controle the printed forms on the templates, in a similar way to the fieldsets used by the django admin.</p>
<p>How to Use:</p>
<p>In the view in question, put:</p>
<pre><code>def some_view(request):
...
fieldsets = (
(u'Title 1',
{'hidden' : ('field_1', 'field_2',),
'fields' : ('field_3',)}),
(u'Title …</code></pre>
Freely redistributableRemember me for login
2010-01-22T07:54:03.091007-06:00iElectrichttps://djangosnippets.org/snippets/1881/<p>Set session to never expire in settings, and when remember_me is found false in login POST, set it to browser session expiry. Works only in Django 1+.</p>
Freely redistributablerouter view
2010-01-19T15:46:25.975208-06:00tokibitohttps://djangosnippets.org/snippets/1876/<p>simple routing views</p>
Freely redistributable