Login

Tag "headers"

Snippet List

Header view decorators

This file includes two Django view decorators `header` and `headers` that provide an easy way to set response headers. 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.

  • views
  • view
  • decorator
  • headers
  • decorators
  • header
Read More
Author: ydm
  • 1
  • 1

Workaround Firefox bug 553888

Firefox transparently follows redirects when AJAX calls return 3xx code. And it drops additional headers, X-Requested-With among them. Server treats redirected HTTP requested as non-AJAX. JS libraries has nothing to do here. At 16.03.11 bug https://bugzilla.mozilla.org/show_bug.cgi?id=553888 has status "NEW" being reported at 21.03.10. Workaround is following: - in process_response(): if request.is_ajax() and response has status_code 3xx then put response["Location"] to session, otherwise unset session stored value (if it is there). - in process_request(): if not request.is_ajax() and request.path equals to stored session value then monkeypatch request.is_ajax() return True (before any views come into play). This results in smooth transparent redirects in Firefox, all treated as AJAX calls.

  • ajax
  • redirect
  • headers
Read More

Add Extra Headers to Test Client Requests

As Simon Willison mentions in his [Debugging Django](http://simonwillison.net/2008/May/22/debugging/) presentation, using the Test Client in the interpreter can be a great way to take a peek at the raw results from a view. In some cases you may need to add additional headers to the request (for instance a piece of middleware may rely on them). Though it is not mentioned in the reference documentation, a quick peek at the code confirmed my hopes that it would be possible to add data to the request. The Client *get* and *post* methods both accept an **extra** kwargs parameter that allow you to populate the request with additional data.

  • testing
  • request
  • test
  • headers
Read More

Timing Django Requests

Adds an 'X-Django-Request-Time' HTTP response header that times how long django spent processing the request.

  • middleware
  • http
  • headers
  • profiling
Read More

Sort Table Headers

Handles creation of `order_by` criteria based on GET parameters and provides context variables to be used when generating table header sort links which respect the current sort field and direction, reversing the direction when the same header is sorted by again. Sample view: from somewhere import SortHeaders from django.contrib.auth.models import User from django.shortcuts import render_to_response LIST_HEADERS = ( ('Username', 'username'), ('First Name', 'first_name'), ('Last Name', 'last_name'), ('Email', None), ) def user_list(request): sort_headers = SortHeaders(request, LIST_HEADERS) users = User.objects.order_by(sort_headers.get_order_by()) return render_to_response('users/user_list.html', { 'users': users, 'headers': list(sort_headers.headers()), }) Sample template: {% load my_tags %} <table cellspacing="0"> <thead> <tr> {% table_header headers %} </tr> </thead> <tbody> {% for user in users %}<tr class="{% cycle odd,even %}"> <td><a href="{{ user.get_absolute_url|escape }}">{{ user.username|escape }}</a></td> <td>{{ user.first_name|escape }}</td> <td>{{ user.last_name|escape }}</td> <td>{{ user.email|escape }}</td> </tr> {% endfor %} </tbody> </table> Sample inclusion tag: from django import template def table_header(context, headers): return { 'headers': headers, } register = template.Library() register.inclusion_tag('table_header.html', takes_context=True)(table_header) Sample inclusion tag template: {% for header in headers %}<th{{ header.class_attr }}> {% if header.sortable %}<a href="{{ header.url|escape }}">{% endif %} {{ header.text }} {% if header.sortable %}</a>{% endif %} </th>{% endfor %}

  • sort
  • table
  • headers
Read More

HTTP headers view decorator

Decorator adding arbitrary HTTP headers to the response. This decorator adds HTTP headers specified in the argument (map), to the HTTPResponse returned by the function being decorated. Example: @headers({'Refresh': '10', 'X-Bender': 'Bite my shiny, metal ass!'}) def index(request): ....

  • http
  • view
  • decorator
  • headers
Read More

7 snippets posted so far.