Skip to content

Instantly share code, notes, and snippets.

@cridenour
Created March 29, 2012 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cridenour/2243612 to your computer and use it in GitHub Desktop.
Save cridenour/2243612 to your computer and use it in GitHub Desktop.
from django.db import models
class Moment(models.Model):
title = models.CharFiend(max_length=128)
image = models.ImageField(upload_to='moments')
description = models.TextField()
uploaded = models.DateTimeField(auto_now_add=True, blank=True)
def __unicode__(self):
return self.title
from django.views.generic import View
import json
from django.shortcuts import HttpResponse
from myapp.models import Moment
class MomentFeed(View):
def get(self, request, *args, **kwargs):
moments = Moment.objects.all().order_by('-uploaded') # Load all moments in reverse order
# Now convert to nice dicts for use in JSON
json_moments = []
for m in moments:
json_moments.append({
'id': m.id,
'title': m.title,
'image': m.description,
})
return HttpResponse(status=200, content_type='application/json', content=json.dumps(json_moments))
@cridenour
Copy link
Author

Totally untested, but point your url in urls.py to the MomentFeed (have to import it fist) to get an idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment