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.2 LTS:
pip install -r requirements-django4.txt
- Django 3 LTS:
-
Edit the
mysite/setting.py
to add your Moesif Application ID.MOESIF_MIDDLEWARE = { 'APPLICATION_ID': 'Your Moesif Collector Application Id', }
After you log into Moesif Portal, you can get your Moesif Application ID during the onboarding steps. You can always access the Application ID any time by following these steps from Moesif Portal after logging in:
a. Select the account icon to bring up the settings menu.
b. Select Installation or API Keys.
c. Copy your Moesif Application ID from the Collector Application ID field.
Also make sure you specify your time zone in the
TIME_ZONE
setting inmysite/settings.py
file. -
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 uvicorn gunicorn
- Django 3 LTS:
-
Be sure to edit the
mysite/setting.py
to add your Moesif Application ID.MOESIF_MIDDLEWARE = { 'APPLICATION_ID': 'Your Moesif Collector Application Id', }
After you log into Moesif Portal, you can get your Moesif Application ID during the onboarding steps. You can always access the Application ID any time by following these steps from Moesif Portal after logging in:
a. Select the account icon to bring up the settings menu.
b. Select Installation or API Keys.
c. Copy your Moesif Application ID from the Collector Application ID field.
Also make sure you specify your time zone in the
TIME_ZONE
setting inmysite/settings.py
file. -
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
, 3.12.3
Tested Django versions: 3.2.13
LTS, 4.2.0
LTS