DEV Community

Aditya Jadhav
Aditya Jadhav

Posted on

Need Help Finalizing My Django-Based Research Study App (CSRF Issue)

Hey Dev Community! ๐Ÿ‘‹

I'm working on a Django-based web application for a university research study that explores passphrases across different languages. Iโ€™ve built most of the core functionality, but Iโ€™ve hit a blocker I can't seem to solve and could use your help.

๐Ÿง  Project Background

This is a user study platform (academic) where participants:

  • Log in or sign up
  • Consent to a study
  • Complete a series of language-based passphrase tasks

We use Django 5.2.1, and the app has:

  • Allauth (for auth, but only the backend currently)
  • Custom user flow
  • Consent form
  • Task-based progression (task1 โ†’ task2, etc.)

โ— Problem I'm Facing

After logging in (especially in a fresh or different browser), when the user clicks โ€œStart the Studyโ€, they hit this:
403 Forbidden: CSRF verification failed. Request aborted.

Despite having:

  • {% csrf_token %} in the form
  • CsrfViewMiddleware active
  • Proper CSRF_TRUSTED_ORIGINS and ALLOWED_HOSTS
  • Cookies confirmed in the browser
  • get_token(request) and @csrf_protect used

Still, the form POST fails on that "Start the Study" step.

โœ… What Works

  • Viewing the home page after login also works.
  • Form displays the token correctly.
  • CSRF cookie is generated.
  • If I refresh or re-login, it sometimes works โ€” it's inconsistent.

๐Ÿ’ก What I Think Might Help

  • Guidance on how to persist CSRF token between views properly?
  • Is get_token(request) necessary in views?
  • Anything to check in my session/cookie setup?

๐Ÿ”— Project Info

  • Python 3.11 / Django 5.2.1
  • SQLite (for now)
  • Hosted locally (localhost:8000)
  • CSRF Cookie: Lax, Secure = False (for dev)
  • CSRF token is present in the form

๐Ÿ“‚ Want to Help?

If youโ€™re interested in helping me wrap this up:

  • I am happy to discuss more via comments here!

Thank you in advance ๐Ÿ™ โ€” this is part of a real academic research study, so your contribution has meaningful impact!

โ€”

P.S.: If youโ€™ve solved a similar issue before, please drop some wisdom! Iโ€™ve debugged it for hours and feel closeโ€ฆ but not quite there.

Top comments (0)