Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please provide example app hierarchy #7

Open
olberger opened this issue Sep 2, 2013 · 1 comment
Open

Please provide example app hierarchy #7

olberger opened this issue Sep 2, 2013 · 1 comment

Comments

@olberger
Copy link

olberger commented Sep 2, 2013

I find it quite hard to create a minimal app using django-conneg.

I think it would be great to have some example app code using it, for instance based on the polls app from the "Writing your first Django app" tutorial.

Thanks in advance.

@olberger
Copy link
Author

olberger commented Sep 2, 2013

I'm not sure this is completely correct, but maybe the following example would help :

import json

from django.http import HttpResponse
from django.template import RequestContext
from django.shortcuts import render_to_response

from django_conneg.decorators import renderer

from django_conneg.views import ContentNegotiatedView

class IndexView(ContentNegotiatedView):

    @renderer(format='json', mimetypes=('application/json',), name='JSON')
    def render_json(self, request, context, template_name):
        # ...
        return HttpResponse(json.dumps(context), mimetype='application/json')

    @renderer(format="html", mimetypes=('text/html', 'application/xhtml+xml'), priority=1, name='HTML')
    def render_html(self, request, context, template_name):
        # ...
        return render_to_response(self.join_template_name(template_name, 'html'),
                                      context, context_instance=RequestContext(request),
                                      mimetype='text/html')


    def get(self, request):
        context = {
            # Build context here
            'foo': "bar",
        }

        # Call render, passing a template name (without file extension)
        print "in get"
        return self.render(request, context, 'index')

It mixes the HTML and JSON and illustrates how to define the template name.

Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant