talpor / django-dashing

django-dashing is a customisable, modular dashboard application framework for Django to visualize interesting data about your project. Inspired in the dashboard framework Dashing
BSD 3-Clause "New" or "Revised" License
731 stars 108 forks source link

How to get data to widget? #80

Closed lehnertz85 closed 7 years ago

lehnertz85 commented 7 years ago

WARINING: I'm fairly new to Django, but I have created a couple other projects. I'm a complete newb when it comes to js.

I can not, for the life of me, get my data to a widget. I've created a widgets.py file. Performed the operations to get the data. I can do print statements and see the correct values. It took me a while to wrap my head around what django-dashing is doing. I think I have it figured out, but I still can't get my custom data to show up on a widget. In fact, the two widgets I've created are blank.

I decided to do some testing and I can't get any data to show on the widgets I've created.

~ view.py


from django.shortcuts import render
from django.template import loader

from django.http import HttpResponse

from models import AccountDim

class CanvasData(Dashboard):
    template_name = 'canvas_dashboard.html'

~ dashing-config.js


myDashboard.addWidget('customWidget', 'Number', {
    getData: function () {
        var self = this;
        Dashing.utils.get('custom_widget', function(data) {
            $.extend(self.scope, data);
        });
    },
    interval: 0
});

myDashboard.addWidget('accountWidget', 'Number', {
    getData: function () {
        var self = this;
        Dashing.utils.get('account_widget', function(data) {
            $.extend(self.scope, data);
        });
    },
    interval: 0
});

In urls.py, I can uncomment #url(r'', include(router.urls)), but my dashboard won't show up, only the default will show.

~ urls.py

from dashing.utils import router

from widgets import NewClientsWidget, AccountWidget
from views import CanvasData
from. import views

router.register(NewClientsWidget, 'custom_widget')
router.register(AccountWidget, 'account_widget')

urlpatterns = [
    url(r'', CanvasData.as_view()),
    #url(r'', views.canvas_dashboard)
    #url(r'', include(router.urls)),
]

~ widgets.py

from dashing.widgets import NumberWidget
from random import randint

from models import AccountDim

users = randint(50, 100)
#AccountDim.objects.count()

class NewClientsWidget(NumberWidget):
    title = 'New Users'

    def get_value(self):
        return '{}'.format(users)

    def get_detail(self):
        global users
        return '{} actives'.format(users/3)

    def get_more_info(self):
        global users
        return '{} fakes'.format(users/10)

class AccountWidget(NumberWidget):
    title = 'Accounts'

    def get_value(self):
        return '{}'.format(users)

    def get_detail(self):
        global users
        return '{} actives'.format(users/3)

    def get_more_info(self):
        global users
        return '{} fakes'.format(users/10)

~ my HMTL template

{% load staticfiles %}

{% block stylesheets %}
{#<link rel="stylesheet" href="{% static 'my/custom/style.css' %}">#}
{% endblock %}

{% block scripts %}
{#<script type="text/javascript" src="{% static '../static' %}"></script>#}
{% endblock %}

{% block config_file %}
<script type="text/javascript" src="{% static 'js/dashing-config.js' %}"></script>
{% endblock %}

I did some test, so there is some commented out code. Any help would be appreciated.

It looks like the routers are just an API which pushes JSON like syntax.

After all this, my guess is it has something to do with the routers. They're not being called? But, if I try to use the routers, I only get the default dashboard. Perhaps the documentation should have a workflow document, so users can understand how data is transferred throughout. Maybe that's naivety.

lehnertz85 commented 7 years ago

Alright, after some testing, my routers are working fine if I change my urls.ps to:

from dashing.utils import router

from widgets import NewClientsWidget, AccountWidget
from views import CanvasData
from. import views

router.register(NewClientsWidget, 'custom_widget')
router.register(AccountWidget, 'account_widget')

urlpatterns = [
    #url(r'dashboard', CanvasData.as_view()),
    #url(r'', views.canvas_dashboard)
    url(r'canvas_dashboard/', include(router.urls)),
]

I can go to localhost:8000/canvas_dashboard/widgets/custom_widget and see the JSON data with my custom data. But, when I navigate to localhost:8000/canvas_dashboard I see the default dashboard and no custom widgets.

If I do a hard shift+refresh my custom js file isn't being loaded, but all the dashing js files are. Including the default dashing-config.js, which is why the sample dashboard is showing and not my custom one.

How do I prevent the sample dashboard from showing? I do have my custom app above dashing in settings.py, which is what the instructions say to do in order to override the supplied dashing-config.js file.

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'canvas_data',
    'dashing',
]

It would appear that isn't working...

EDIT: Some more food for thought: I don't think my template HTML file is being called/summoned. I have it listed in views.py, but views aren't being used in urls.py like you would normally do. The only reason I have a view.py file is because I tried following the multi-dashboard example to see if I could get that to work.