Django app as Facebook canvas app
As a newbie to Django, getting an empty page in the Facebook canvas tricked me up quite a bit. Here are some reasons for getting a blank canvas, even though your app works when accessed directly.
When you load a website through a Facebook canvas URL (https://apps.facebook.com/your_app_name), the request to your app is POSTed. By default, Django apps reject POST requests that donât contain a âcsrf_tokenâ with a â403 Forbiddenâ error.
To disable this behavior, remove âdjango.middleware.csrf.CsrfViewMiddlewareâ from your INSTALLED_APPS list. Obviously this will open your app up to XSS attacks, so make sure to use the csrf_protect decorator on views that accept POST requests from your own app.
After âfixingâ the above problem I expected things to just work. Unfortunately, Django provides another hurdle protection in the form of its clickjacking protection. By default, Django apps send a response header like âX-Frame-Options: SAMEORIGINâ. This forces modern browsers to not embed the Django app in a frame or iframe, such as inside the Facebook canvas. Even though the HTTP request to your Django app returns â200 OKâ, modern browsers just wonât display its contents inside Facebookâs frame.
Fortunately there is an easy way to disable this, too - just remove âdjango.middleware.clickjacking.XFrameOptionsMiddlewareâ from your MIDDLEWARE_CLASSES.
Once again, I cannot stress enough that removing any of these middlewares opens your app up to potential attacks, so consider the consequences carefully before resorting to these solutions.