This tutorial will help you integrate Flexmonster with the Django framework.
To run a simple application, you will need Python and Django. Get the latest Python and Django versions if they’re not already installed on your machine.
After that, choose one of the following options:
Step 1. Download the .zip
archive with the sample or clone it from GitHub with the following command:
git clone https://github.com/flexmonster/pivot-Django pivot-django
cd pivot-django
Step 2. Run your application:
python manage.py runserver
To see the result, open http://localhost:8000/
in your browser.
The application can be shut down manually with Ctrl+C
.
To integrate Flexmonster into a Django app, follow these steps:
Step 1. If you don’t have a Django project, you can create it by running these commands in the console:
django-admin startproject pivot_django
cd pivot_django
Step 2. In the project, create a new app with the following command:
python manage.py startapp pivot_django_app
Step 3. Register the app in the project by adding the app’s config function’s name (this can be found in pivot_django_app/apps.py
) to the INSTALLED_APPS
list in the pivot_django/settings.py
file:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pivot_django_app.apps.PivotDjangoAppConfig',
]
Step 4. Create a folder (e.g., templates/
) for HTML templates in the root folder where pivot_django
and pivot_django_app
are located. Next, create an HTML file (e.g., index.html
) and add Flexmonster to it:
<h1>Flexmonster integration with Django</h1>
<div id="pivotContainer"></div>
<script src="https://cdn.flexmonster.com/flexmonster.js"></script>
<script>
var pivot = new Flexmonster({
container: "pivotContainer",
componentFolder: "https://cdn.flexmonster.com/",
report: {
dataSource: {
type: "json",
filename: "https://cdn.flexmonster.com/data/data.json"
}
}
});
</script>
Step 5. Add the created HTML page to the app’s views. Navigate to pivot_django_app/views.py
and insert the following index
function there:
def index(request):
return render(request, 'index.html')
Step 6. Add the path('', views.index, name='index'),
line to the urlpatterns
list in pivot_django/urls.py
:
from pivot_django_app import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
]
Step 7. Add an os.path.join(BASE_DIR, 'templates')
line to the DIRS
list in the TEMPLATES
list in pivot_django/settings.py
:
'DIRS': [os.path.join(BASE_DIR, 'templates')],
Step 8. Run your application:
python manage.py runserver
To see the result, open http://localhost:8000/
in your browser.
The application can be shut down manually with Ctrl+C
.
You may be interested in the following articles: