djangosnippets.org: Latest snippets tagged with 'request'https://djangosnippets.org/tags/request/2015-07-01T10:18:21.881120-05:00Inject request into functions
2015-07-01T10:18:21.881120-05:00lorinkozhttps://djangosnippets.org/snippets/10505/<p>Long story short:</p>
<ul>
<li>
<p>Django lets you call functions in templates, but you can't pass any parameters.</p>
</li>
<li>
<p>Sometimes you need to use the request object to perform certain tasks, such as determining whether the current user has permission to do something.</p>
</li>
<li>
<p>The recommended approach is to call functions that require parameters …</p></li></ul>
Freely redistributableGet typed dictionary from request GET or POST prameters (MergeDict)
2013-02-01T14:41:22.480002-06:00pahazhttps://djangosnippets.org/snippets/2886/<p>Позволяет получить типизированный словарь из входных параметров.</p>
<p>Может быть использован, например, для дальнейшей передаче параметров в objects.filter(**rez).</p>
Freely redistributableAdd querystring parameters to path (template tag)
2013-01-23T11:39:45.767733-06:00spenoirhttps://djangosnippets.org/snippets/2882/<p><code><h3>Page: {{ page.number }} of {{ page.paginator.num_pages }}</h3>
{% if page.has_previous or page.has_next %}
<div>
{% if page.has_previous %}
<a href="{% url_add_query page=page.previous_page_number %}">{% endif %}&laquo;
Previous
{% if page.has_previous %}</a>{% endif %}
|
{% if page.has_next %}
<a href="{% url_add_query page=page.next_page_number %}">{% endif %}
Next &raquo;{% if page.has_next %}</a>{% …</code></p>
Freely redistributableAdd get_addr() method to request object
2012-12-13T17:17:26.955015-06:00nikmolnarhttps://djangosnippets.org/snippets/2863/<p>I thought it would be useful to have a <code>get_addr()</code> method available on request objects, similar to the <code>get_host()</code> provided by Django. This middleware will add a <code>get_addr()</code> method to requests which uses the <code>X-Forwarded-For</code> header (useful if you're behind a proxy) if it's present and you have the <code>USE_X_FORWARDED_FOR …</code></p>
Freely redistributableGlobalRequest middleware
2012-11-26T12:37:20.565230-06:00myqhttps://djangosnippets.org/snippets/2853/<p>Thread-safe middleware that makes the current <code>request</code> object available globally.</p>
Freely redistributableanother request logging middleware with request time and extra info
2011-12-11T09:26:47.236978-06:00yoavhttps://djangosnippets.org/snippets/2624/<p>Simple logging middleware that captures the following:
* remote address (whether proxied or direct)
* if authenticated, then user email address
* request method (GET/POST etc)
* request full path
* response status code (200, 404 etc)
* content length
* request process time
* If DEBUG=True, also logs SQL …</p>
Freely redistributableGet Client IP Behind Proxy
2011-10-21T09:35:30.037659-05:00brianjaystanleyhttps://djangosnippets.org/snippets/2575/<p>If your application server is behind a proxy, <code>request.META["REMOTE_ADDR"]</code> will likely return the proxy server's IP, not the client's IP. The proxy server will usually provide the client's IP in the <code>HTTP_X_FORWARDED_FOR</code> header. This util function checks both headers. I use it behind Amazon's Elastic Load Balancer (ELB).</p>
Freely redistributablecreate_c
2011-08-24T15:03:53.269601-05:00catellarhttps://djangosnippets.org/snippets/2527/<p>Instead of creating a dictionary on every view everytime you could do this</p>
<p>and just call it like c = create_c(request)</p>
Freely redistributableAdd GET parameters from current request
2011-05-04T12:10:29.520357-05:00naktinishttps://djangosnippets.org/snippets/2428/<p>The tag generates a parameter string in form '?param1=val1&param2=val2'.
The parameter list is generated by taking all parameters from current
request.GET and optionally overriding them by providing parameters to the tag.</p>
<p>This is a cleaned up version of http://djangosnippets.org/snippets/2105/. It
solves a couple of issues, namely:
* parameters are optional …</p>
Freely redistributableRequestFetchingMixin
2011-01-26T11:51:55.617797-06:00eternicodehttps://djangosnippets.org/snippets/2336/<p>More a proof of concept than anything, this mixin addresses a perceived "shortcoming" of django forms: the inability to interact with the current request, and specifically the "current user".
</p>
<p>Usage:
</p>
<pre><code>class SomeForm(forms.Form, RequestFetchingMixin):
# fields...
def __init__(self, *args, **kwargs):
if self.request:
# interact with self.request?
super(SomeForm, self).__init__(*args, **kwargs)
if self.request: …</code></pre>
Freely redistributableMonkey-patch Django's test client to return WSGIRequest objects
2010-11-12T11:11:45.721242-06:00robmadolehttps://djangosnippets.org/snippets/2259/<p>Testing low-level functionality sometimes requires a WSGIRequest object. An example of this is testing template tags.
</p>
<p>This will monkey-patch the test Client object to return WSGIRequest objects
</p>
<p>Normal Django behavior:
</p>
<pre><code>>>> client.get('/')
<HttpResponse >
</code></pre><p>With this code, get the request object:
</p>
<pre><code>>>> client.request_from.get('/')
<WSGIRequest >
</code></pre><p>Installation:
</p>
<p>For this to work, …</p>
Freely redistributableModify requests in your unit tests (improvement on RequestFactory)
2010-10-07T17:21:44.816144-05:00vaughnkochhttps://djangosnippets.org/snippets/2231/<p>This is an update to Simon Willison's snippet http://djangosnippets.org/snippets/963/, along with one of the comments in that snippet.<br />
</p>
<p>This class lets you create a Request object that's gone through all the middleware. Suitable for unit testing when you need to modify something on the request directly, or pass in a …</p>
Freely redistributableDoing redirect without request
2010-09-23T08:55:10.918970-05:00divermanhttps://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 redistributableGet current user without a request object
2010-08-30T04:51:09.100236-05:00t_rybikhttps://djangosnippets.org/snippets/2179/<p>Mechanism to obtain a <code>request.user</code> object without the <code>request</code> object itself. Requires <code>LocalUserMiddleware</code> in <code>MIDDLEWARE_CLASSES</code> settings variable.
</p>
<p><strong>Important</strong>: works under assumption that within a web server each request is handled by a separate thread (as for example in the Apache HTTP server).
</p>
<p><strong>Beware</strong>: <a href="http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser">security threat</a>, although <a href="http://groups.google.com/group/django-users/browse_thread/thread/e7af359d7d183e04">"thread …</a></p>
Freely redistributableTemplate tag which gets specific GET variables from the current request
2010-03-21T09:02:53.781214-05:00aruseni.magikuhttps://djangosnippets.org/snippets/1965/<p>This template tag attempts to get specific GET variables from request.GET. If such variables exist, it shows them as a query string (with optional "include_ampersand" mode when it puts an "&" at the end if there is a result string, or a "?" if there's none: it is used when …</p>
Freely redistributable