djangosnippets.org: Latest snippets tagged with 'http'http://djangosnippets.org/tags/http/2015-03-09T10:07:22.419835-05:00Testing (and mocking) http requests to APIs etc.
2015-03-09T10:07:22.419835-05:00monikasulikhttp://djangosnippets.org/snippets/10470/<p>You can test how your django app behaves depending on what kind of response it gets from the API. It assumes were're using the python requests library.</p>
Freely redistributableApache X-sendfile with permissions checking
2012-03-31T09:56:32.247259-05:00h0axifyhttp://djangosnippets.org/snippets/2728/<p>This allows the mod_xsendfile module for Apache safely serving private files. Django take cake about processing and permissions checking, Apache server requested files.</p>
<p>Installation of mod_xsendfile:</p>
<p>$ tar -xzvf mod_xsendfile-0.12.tar.gz
$ /usr/sbin/apxs -c mod_xsendfile-0.12/mod_xsendfile.c
$ ld -Bshareable -o mod_xsendfile-0.12/mod_xsendfile.so mod_xsendfile-0.12/mod_xsendfile.o</p>
<p>Copy mod_xsendfile.so to your local Apache modules folder.
Modify httpd.conf …</p>
Freely redistributableTLS(SSL) middleware, per URL pattern or whole site
2010-12-01T08:46:29.250707-06:00robmadolehttp://djangosnippets.org/snippets/2282/<p>Allows url patterns to include a boolean indicating whether a view requires
TLS(SSL). The accompanying middleware handles the redirects needed to make
sure that it upholds this requirement.
</p>
<p><strong>WARNING</strong>: this monkey-patches some Django internals and is difficult to test
since Django's TestClient does not support TLS. If you use …</p>
Freely redistributableDoing redirect without request
2010-09-23T08:55:10.918970-05:00divermanhttp://djangosnippets.org/snippets/2204/<p>When you neeed to do redirect and request object is not available, you can do it with exception.</p>
<p>Put exception handler somewhere request is available, for example to middleware or ModelAdmin.</p>
<p>Raise exception, where request is not available.</p>
Freely redistributableSet language via HTTP GET Parameter
2010-03-02T04:44:00.525636-06:00schmidsihttp://djangosnippets.org/snippets/1948/<p>Two middlewares to handle languages via HTTP GET
</p>
<p>original code by stefan reinhard, check against django.conf.settings by me
</p>
Freely redistributableSupport alternative authentication mechanisms with Piston
2010-01-15T23:20:58.885141-06:00erikwrighthttp://djangosnippets.org/snippets/1871/<p>This snippet for Piston allows you to offer a choice of authentication methods to clients.
</p>
Freely redistributableDigest authentication for Piston
2010-01-15T23:14:52.422198-06:00erikwrighthttp://djangosnippets.org/snippets/1870/<p>This simple class allows you to use django-digest (http://bitbucket.org/akoha/django-digest/) with Piston.</p>
Freely redistributableSimple views dispatcher by http methods
2009-11-16T05:09:11.381511-06:00kmerenkovhttp://djangosnippets.org/snippets/1799/<p>Calls a view by request.method value.</p>
<p>To use this dispatcher write your urls.py like this:</p>
<pre><code>urlpatterns = pattern('',
url(r'^foo/$', dispatch(head=callable1,
get=callable2,
delete=callable3)),
)
</code></pre>
<p>If <code>request.method</code> is equal to head, <code>callable1</code> will be called as your usual view function;
if it is <code>get</code>, <code>callable2</code> will be called; et cetera.
If …</p>
Freely redistributableGeneric view 'redirect_to' that supports QUERY_STRING
2009-10-06T11:17:29.607698-05:00marinhohttp://djangosnippets.org/snippets/1752/<p>This generic view does the same that 'django.views.generic.simple.redirect_to' does but supports request.GET parameters.</p>
Freely redistributableRESTful class dispatch
2009-06-19T15:35:16.278200-05:00Phoenixhttp://djangosnippets.org/snippets/1582/<p>Yet another implementation of class based RESTful dispatch. This particular implementation features:</p>
<ul>
<li>You do not have to call <strong>init</strong> from the derived classes.</li>
<li>Avoids <strong>metaclass</strong> which (in our environment) led to unexpected method override behavior.</li>
<li>Method names match the google webapp API.</li>
<li>One new instance per request to reduce errors …</li></ul>
Freely redistributableBase class for RESTful Views
2008-11-30T22:41:55.734923-06:00jpwattshttp://djangosnippets.org/snippets/1226/<p>Subclass <code>Resource</code> to create a view that will dispatch based on the HTTP method of the request.
</p>
<pre><code>class View(Request):
def DELETE(self, request):
...
def GET(self, request):
...
def PUT(self, request):
...
</code></pre><p>Other snippets provided inspiration:
</p>
<ul>
<li>
<a href="http://www.djangosnippets.org/snippets/436/">436</a>
</li>
<li>
<a href="http://www.djangosnippets.org/snippets/437/">437</a>
</li>
<li>
<a href="http://www.djangosnippets.org/snippets/1071/">1071</a>
</li>
<li>
<a href="http://www.djangosnippets.org/snippets/1072/">1072</a>
</li>
</ul>
<p>The code is also available on <a href="http://github.com/jpwatts/django-restviews/">GitHub</a>.
</p>
Freely redistributableHTTP authentication using your ModelBackend
2008-11-15T09:06:14.787925-06:00dipankarsarkarhttp://djangosnippets.org/snippets/1192/<p>Save this file as httpauth.py. This example should help you guys out for sure.</p>
<pre><code>from httpauth import *
@logged_in_or_basicauth()
def temp_view(request):
pass
</code></pre>
<p>Feel free to contact me in case you need help.
By <a href="http://dipankar.name">Dipankar sarkar</a>
me@dipankar.name</p>
Freely redistributableMessage exception
2008-10-14T12:31:31.903374-05:00marinhohttp://djangosnippets.org/snippets/1143/<p>This exception is util when you want to raise an exception but want its message be shown as a message to the user, with no error 500 or 404 pages.</p>
<p>To use it, just append the middleware in the MIDDLEWARE_CLASSES setting and raises HttpMessage when necessary.</p>
Freely redistributableTiming Django Requests
2008-06-12T09:38:04.836610-05:00metajackhttp://djangosnippets.org/snippets/797/<p>Adds an 'X-Django-Request-Time' HTTP response header that times how long django spent processing the request.</p>
Freely redistributableHTTP method_required Decorator
2008-03-29T09:24:35.563513-05:00AdamKGhttp://djangosnippets.org/snippets/674/<p>Edit: As James pointed out, <code>django.views.decorators.http</code> already provides stuff for this. Use that instead.</p>
<p>Old description: Should be pretty straightforward; you give it the method you can accept, it returns 405's for other methods. EG, <code>@method_required("POST")</code> at the top of your view. </p>
Freely redistributable