from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

from django.contrib.auth.models import User
from django.shortcuts import render_to_response
from rest_framework import serializers, viewsets


# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'is_staff')


# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer


def home_page(request):
    return render_to_response('home/home.html')


def compare(request):
    return render_to_response('home/compare.html')

from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

from django.contrib.auth.models import User
from django.shortcuts import render_to_response
from django.template.context_processors import csrf
from rest_framework import serializers, viewsets


# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'is_staff')


# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer


def home_page(request):
    csrf_token = {}
    csrf_token.update(csrf(request))

    return render_to_response('home/home.html', csrf_token)


def compare(request):
    return render_to_response('home/compare.html')
Return a csrf token on the homepage.
Return a csrf token on the homepage.

We were not setting a CSRF token on the homepage. This meant that requests to
API endpoints did not have a token available. This change sets the token
immediatley as part of the cookie. Ajax calls can then use this value.

  from __future__ import print_function
  from __future__ import unicode_literals
  from __future__ import division
  
  from django.contrib.auth.models import User
  from django.shortcuts import render_to_response
+ from django.template.context_processors import csrf
  from rest_framework import serializers, viewsets
  
  
  # Serializers define the API representation.
  class UserSerializer(serializers.HyperlinkedModelSerializer):
      class Meta:
          model = User
          fields = ('url', 'username', 'email', 'is_staff')
  
  
  # ViewSets define the view behavior.
  class UserViewSet(viewsets.ModelViewSet):
      queryset = User.objects.all()
      serializer_class = UserSerializer
  
  
  def home_page(request):
+     csrf_token = {}
+     csrf_token.update(csrf(request))
+ 
-     return render_to_response('home/home.html')
+     return render_to_response('home/home.html', csrf_token)
  
  
  def compare(request):
      return render_to_response('home/compare.html')
- 
Return a csrf token on the homepage.
## Code Before:
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

from django.contrib.auth.models import User
from django.shortcuts import render_to_response
from rest_framework import serializers, viewsets


# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'is_staff')


# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer


def home_page(request):
    return render_to_response('home/home.html')


def compare(request):
    return render_to_response('home/compare.html')

## Instruction:
Return a csrf token on the homepage.
## Code After:
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

from django.contrib.auth.models import User
from django.shortcuts import render_to_response
from django.template.context_processors import csrf
from rest_framework import serializers, viewsets


# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'is_staff')


# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer


def home_page(request):
    csrf_token = {}
    csrf_token.update(csrf(request))

    return render_to_response('home/home.html', csrf_token)


def compare(request):
    return render_to_response('home/compare.html')