Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have addressed this by creating a generic view class, based on Django's own generic.View class, that defines a decorator 'accept_types'. This modifies the view to which it is applied so that it returns None if the indicated content-type is not in the Accept header. Then, the get() method (which is called by the generic.View dispatcher) looks like this:</p> <pre><code>def get(self, request): self.request = request # For clarity: generic.View does this anyway resultdata = { 'result': data, etc. } return ( self.render_uri_list(resultdata) or self.render_html(resultdata) or self.error(self.error406values()) ) </code></pre> <p>The actual view renderers are decorated thus:</p> <pre><code>@ContentNegotiationView.accept_types(["text/uri-list"]) def render_uri_list(self, resultdata): resp = HttpResponse(status=200, content_type="text/uri-list") # use resp.write(...) to assemble rendered response body return resp @ContentNegotiationView.accept_types(["text/html", "application/html", "default_type"]) def render_html(self, resultdata): template = loader.get_template('rovserver_home.html') context = RequestContext(self.request, resultdata) return HttpResponse(template.render(context)) </code></pre> <p>The (one-off) generic view class that declares the decorator looks like this:</p> <pre><code>class ContentNegotiationView(generic.View): """ Generic view class with content negotiation decorators and generic error value methods Note: generic.View dispatcher assigns HTTPRequest object to self.request. """ @staticmethod def accept_types(types): """ Decorator to use associated function to render the indicated content types """ def decorator(func): def guard(self, values): accept_header = self.request.META.get('HTTP_ACCEPT',"default_type") accept_types = [ a.split(';')[0].strip().lower() for a in accept_header.split(',') ] for t in types: if t in accept_types: return func(self, values) return None return guard return decorator </code></pre> <p>(The parameter handling in the decorator should be generalized - this code works, but is still in development as I write this. The actual code is in GitHub at <a href="https://github.com/wf4ever/ro-manager/tree/develop/src/roverlay/rovweb/rovserver" rel="nofollow">https://github.com/wf4ever/ro-manager/tree/develop/src/roverlay/rovweb/rovserver</a>, but in due course should be separated to a separate package. HTH.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload