views.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # Create your views here.
  2. from django.template import Context, loader
  3. from django.http import HttpResponse
  4. from django.core import serializers
  5. from world.models import World, Fortune
  6. from django.shortcuts import render
  7. import ujson
  8. import random
  9. from operator import attrgetter
  10. def json(request):
  11. response = {
  12. "message": "Hello, World!"
  13. }
  14. return HttpResponse(ujson.dumps(response), mimetype="application/json")
  15. def db(request):
  16. queries = int(request.GET.get('queries', 1))
  17. worlds = []
  18. for i in range(queries):
  19. # get a random row, we know the ids are between 1 and 10000
  20. worlds.append(World.objects.get(id=random.randint(1, 10000)))
  21. return HttpResponse(serializers.serialize("json", worlds), mimetype="application/json")
  22. def fortunes(request):
  23. fortunes = list(Fortune.objects.all())
  24. fortunes.append(Fortune(id=0, message="Additional message added at runtime."))
  25. fortunes = sorted(fortunes, key=attrgetter('message'))
  26. context = {'fortunes': fortunes}
  27. return render(request, 'fortunes.html', context)