Django is a web application framework that many developers use to serve APIs.
Moesif is an API analytics and monitoring platform. moesifdjango is a middleware that makes integration with Moesif easy for Django based applications.
This is an example of django application with Moesif integrated. This example is based on the quick start tutorials of Django and Django rest framework.
moesifdjango's github readme already documented the steps for setup Moesif Django. But here is the key file where the Moesif integration is added:
mysite/settings.py
added Moesif middleware related settings.
-
Setup virtual env if needed
python -m venv ENV
. Start the virtual env bysource ENV/bin/activate
-
Install packages:
- Django 3 LTS:
pip install -r requirements.txt
- Django 4:
pip install -r requirements-django4.txt
- Be sure to edit the
mysite/setting.py
to add your Moesif application id.
MOESIF_MIDDLEWARE = {
'APPLICATION_ID': 'Your Moesif Collector Application Id',
}
Your Moesif Collector Application Id can be found in the Moesif Portal. After signing up for a Moesif account, your Moesif Application Id will be displayed during the onboarding steps.
You can always find your Moesif Collector Application Id at any time by logging into the Moesif Portal, click on the bottom left user profile, and then clicking API Keys.
- Run the service:
python manage.py runserver
- See
urls.py
for some urls that you can hit the server with (e.g.http://localhost:8000/users
), and the data should be captured in the corresponding Moesif account of the application id.
-
Setup virtual env if needed
python -m venv ENV
. Start the virtual env bysource ENV/bin/activate
-
Install packages:
- Django 3 LTS:
pip install -r requirements.txt
- Django 4:
pip install -r requirements-django4.txt
Also install dependencies to related to asgi
pip install --no-cache asgi uvicorn gunicorn
- Be sure to edit the
mysite/setting.py
to add your Moesif application id.
MOESIF_MIDDLEWARE = {
'APPLICATION_ID': 'Your Moesif Collector Application Id',
}
Your Moesif Collector Application Id can be found in the Moesif Portal. After signing up for a Moesif account, your Moesif Application Id will be displayed during the onboarding steps.
You can always find your Moesif Collector Application Id at any time by logging into the Moesif Portal, click on the bottom left user profile, and then clicking API Keys.
- Run the service:
gunicorn mysite.asgi:application -k uvicorn.workers.UvicornWorker
- See
urls.py
for some urls that you can hit the server with (e.g.http://localhost:8000/users
), and the data should be captured in the corresponding Moesif account of the application id.
Note : If you get OperationalError
with Exception Value: no such table: auth_user
, that means the schema has not been generated in database yet.
please run the following command line to resolve
python manage.py migrate
Tested Python versions: 3.10.4
Tested Django versions: 3.2.13 (LTS)
, 4.0.5