simonw / django-sql-dashboard

Django app for building dashboards using raw SQL queries
https://django-sql-dashboard.datasette.io/
Apache License 2.0
437 stars 37 forks source link

It is almost possible to embed the dashboard views in a Django admin template #114

Open toolness opened 3 years ago

toolness commented 3 years ago

Hello again! I've got the dashboard up and running on my site and it's working great.

One thing I'd like to do, if possible, is style the dashboard views so that they look like they're part of the admin site (since on my site, they are accessible only to staff, from the Django admin). On a lark, I tried setting my django_sql_dashboard/base.html to the following:

{% extends "admin/base_site.html" %}
{% load static %}

{% block extrahead %}
<style>{% include "django_sql_dashboard/_css.html" %}</style>
{% endblock %}

This actually came really close to working! I'm not sure if it was intentional on your part, but django-sql-dashboard's custom CSS melds with the admin site's CSS quite nicely:

image

There are two problems, though:

  1. The template isn't being passed the context of AdminSite.each_context(), so a bunch of stuff that's usually shown in the header isn't being shown. If there were a way to pass custom context to the dashboard views, it would be awesome.

  2. The title displays twice, I feel like there should be an easy way around this, but the only thing I can think of right now is a horrible CSS hack:

    h1 ~ h1 {
      display: none;
    }

Anyways, I guess that means (1) is the only real blocker for me. Looking at django-sql-dashboard's code, I can't see any easy way of injecting extra context into the dashboard views. One possibility I can think of might be to add optional extra_context arguments to the views, which I'd be happy to submit a PR for. Anyways, let me know if you have any thoughts!

simonw commented 3 years ago

Having this as a documented, supported way of using the tool makes a ton of sense to me. I like the pattern of wrapping the Django SQL Dashboard view with a custom view to add extra context - an extra_context= argument sounds like it could help there.

I don't understand why the title shows twice, but we should definitely find a fix for that!

PRs welcome for this.

toolness commented 3 years ago

Hey, I just noticed that you mentioned this over in https://github.com/simonw/django-sql-dashboard/issues/127#issuecomment-858756778:

I think the way to address this would be to switch over to more of a class-based-view approach to allow people to subclass the dashboard views and implement things like their own custom permissions.

While this issue isn't directly related to that one, I think that a good solution to this one might be the same kind of thing--moving the views over to more of a class-based-view approach that would allow these sorts of things to be easily customized. Anyways, just a random thought I figured I'd log here.