How to use context field?
Closed this issue · 2 comments
elizavetabyn commented
Hi, can you please describe how to use context field in my project? Can't understand how to add data into it
hugorodgerbrown commented
Hi @atanassia - if you want to add data to it you should add a function to your Django settings called
USER_VISIT_REQUEST_CONTEXT_EXTRACTOR
that takes HttpRequest
as the only arg, and that returns a dict, which is stored in the context field.
e.g. this would store whether the user was a staff user:
# settings.py
USER_VISIT_REQUEST_CONTEXT_EXTRACTOR = lambda r: { "is_staff": r.user.is_staff }
If you want to do something more complicated that you can't achieve in a lambda, you can define the function and then just assign it to the setting:
# settings.py
def really_complicated_function(request: HttpRequest) -> dict:
...
return { ... }
USER_VISIT_REQUEST_CONTEXT_EXTRACTOR = really_complicated_function
elizavetabyn commented
thank you