djangosnippets.org: Latest snippets tagged with 'class-based-views'https://djangosnippets.org/tags/class-based-views_1/2014-08-27T05:01:06.289396-05:00Middleware decoratores for classed based views
2014-08-27T05:01:06.289396-05:00dhkehttps://djangosnippets.org/snippets/3091/<p>Decorators to attach middleware to class based views w/o arguments.</p>
Freely redistributableBasic PDF view mixin and utils using reportlab.
2013-07-19T02:13:35.464470-05:00elequhttps://djangosnippets.org/snippets/2975/<p>Simplified version of the snippet that renders model to PDF <a href="http://djangosnippets.org/snippets/2540/">http://djangosnippets.org/snippets/2540/</a> </p>
<p>This PDF view mixin for Django Class Based Views.</p>
<p>See working project example: https://github.com/elena/django-pdfmixin-example</p>
<hr />
<p>This is based on the case scenario where you have a model which has a <code>DetailView</code>.</p>
<p>You then construct a bespoke PDF for the …</p>
Freely redistributableClass-based view mixin for flatpages
2013-02-26T07:56:24.001204-06:00schwukhttps://djangosnippets.org/snippets/2896/<p>Allows you to include content from flatpages in class-based views. You can specify the url for the flatpage you want, or let it be determined by request.path.</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 redistributableDecorating class-based views
2012-01-30T17:36:49.377143-06:00lqchttps://djangosnippets.org/snippets/2668/<p>This is a simplest approach possible. <code>as_view()</code> is replaced, so
that it applies the given decorator before returning.</p>
<p>In this approach, decorators are always put on top - that means it's not
possible to have functions called in this order:</p>
<pre><code>B.dispatch, login_required, A.dispatch
</code></pre>
<p>NOTE: By default this modifies the given …</p>
Freely redistributableClass-based process form view.
2011-12-05T09:44:33.980714-06:00I159https://djangosnippets.org/snippets/2623/<p>Class based view returns json serialized saved data or form errors.</p>
Freely redistributableDownloadView generic class view
2011-09-17T21:18:58.967784-05:00ckniffenhttps://djangosnippets.org/snippets/2549/<p>Generic class view to abstract out the task of serving up files from within Django.
Recommended usage is to combine it with SingleObjectMixin and extend certain methods based on your particular use case.</p>
<p>Example usage</p>
<pre><code> class Snippet(models.Model):
name = models.CharField(max_length = 100)
slug = SlugField()
code = models.TextField()
from django.views.generic.detail …</code></pre>
Freely redistributableView mixin and utils to generate PDF documents from html using xhtml2pdf
2011-09-06T10:17:53.590136-05:00frankbanhttps://djangosnippets.org/snippets/2540/<p>View mixin and utils to generate PDF documents from html using <em>xhtml2pdf</em>.</p>
<p>The most interesting thing here is <em>PDFTemplateResponseMixin</em>.
Adding this mixin to class based views allows automatic pdf generation using
the view context and a customized template.</p>
<p>There is also the lower level function <em>render_to_pdf</em>, similar …</p>
Freely redistributableClass based view decorator
2011-08-05T03:55:51.308729-05:00robcharlwoodhttps://djangosnippets.org/snippets/2505/<p>Converts a passed decorator to one that can be applied to a class based view (ie. automatically decorates dispatch). This means no more overriding dispatch for every view / request method you want to apply decorators to.</p>
<p>Works in Django 1.3 but I suspect it probably works in 1.2 as …</p>
Freely redistributableTaskViewMixin, fires off a task and polls until completion
2011-08-01T20:25:02.920191-05:00zbyte64https://djangosnippets.org/snippets/2500/<p>TaskViewMixin can be mixed in with a Class Based view and handles the scheduling and polling of a task. During task execution, a waiting page is rendered that should refresh itself. Once the task is complete, the view may then render a success page and can collect the payload of …</p>
Freely redistributableLoginRequired class-based view decorator
2011-07-25T02:25:15.007512-05:00mjumbehttps://djangosnippets.org/snippets/2495/<p>Apply the <code>login_required</code> decorator to all the handlers in a class-based view that delegate to <code>cls.dispatch</code>.</p>
<p>Optional arguments:</p>
<ul>
<li>redirect_field_name = <code>REDIRECT_FIELD_NAME</code></li>
<li>login_url = <code>None</code></li>
</ul>
<p>See the documentation for the <a href="https://docs.djangoproject.com/en/dev/topics/auth/#the-login-required-decorator"><code>login_required</code></a> method for more information about the keyword arguments.</p>
<p>Usage:</p>
<pre><code>@LoginRequired
class MyListView (ListView):
...
</code></pre>
Freely redistributable