pawamoy / pawamoy.github.io

http://pawamoy.github.io/
2 stars 0 forks source link

posts/django-dashboard-with-suit-and-highcharts/ #23

Closed utterances-bot closed 2 years ago

utterances-bot commented 4 years ago

Django admin dashboard with Suit and Highcharts - pawamoy's website

Findings, thoughts, tutorials, work. Pieces of my mind!

https://pawamoy.github.io/posts/django-dashboard-with-suit-and-highcharts/

pawamoy commented 4 years ago

Original author: Gustavo
Original date: 2016-04-26T15:59:49Z

Thanks for post :)

Using Django 1.9.5, you need to declare something like (in urls.py):


admin.site = DashboardSite()
admin.sites.site = admin.site
admin.autodiscover()


Otherwise, Django's native applications, such as 'django.contrib.auth' and 'django.contrib.sites', will not be identified.

pawamoy commented 4 years ago

Original date: 2016-05-04T09:30:28Z

Thank you Gustavo, I added your comment as an edit in the post :)

pawamoy commented 4 years ago

Original author: gdr
Original date: 2016-08-01T18:13:38Z

Great post! Is it possible to use in a custom AdminSite? I've tried the above, but am getting "You don't have permission to edit anything." when trying to open the custom site after logging in

pawamoy commented 4 years ago

Original date: 2016-08-01T18:45:57Z

Thank you!

Okay so if you want to use this without django-suit, you have to play between 'django.contrib.admin' and 'django.contrib.admin.apps.SimpleAdminConfig' in your installed apps.
I was indeed trying to decouple this from suit and I encountered the same issue as you do.
One of these admin will let you have access (permissions) to edit the elements, but then you will have to manually code the template to display the link to the models (you can get inspiration looking at the
official django templates for the admin site).

You could also check the django-app I wrote, starting from this post: https://github.com/Pawamoy/...
It is way better than this post's contents, but be careful because the documentation is not in sync with the version released on PyPi.

pawamoy commented 4 years ago

Original author: gdr
Original date: 2016-08-01T18:59:39Z

Thanks for the speedy response! I've tried both 'django.contrib.admin' and 'django.contrib.admin.apps.SimpleAdminConfig' without any luck. I am using django-suit too, but for 2 admin sites- admin and custom_admin. It looks like it might be an issue with app_list context var for custom_admin site possibly (or is this something I need to create in the view myself?) as it's still possible to navigate to to the models that have been custom_admin.register(). I'll play around with templates though, as I've only been extending the dashboard off suit's admin/index.html at the moment as a starting point. I'm also using a custom AUTH_USER_MODEL, so not sure if that would affect things?

pawamoy commented 4 years ago

Original date: 2016-08-01T19:14:00Z

You're welcome. Okay so you have 2 sites. Maybe an issue with admin.sites.site = admin.site?

You could check what differs in your custom admin from the one I show in this post...
I didn't play much with custom AdminSite so I can't really tell you what to search for ^^'
And I always avoided changing AUTH_USER_MODEL haha, but can't tell either if this is an issue!

Good luck ! (but don't hesitate to ask me more if you think I can help ;))

pawamoy commented 4 years ago

Original author: gdr
Original date: 2016-08-01T20:59:23Z

Thanks again, I'm quite new to django, so pretty sure I was missing something simple, but in the end for my purposes I landed up doing:

class CustomAdmin(admin.AdminSite):
    def get_urls(self):
        from django.conf.urls import url
        urls = super(CustomAdmin, self).get_urls()
        urls += [
            url(r'^custom-admin/$', self.admin_view(self.DashboardView))
        ]
        return urls

    class DashboardView(TemplateView):
        template_name = 'admin/dashboard/welcome.html'

        def get(self, request, *args, **kwargs):
            context = self.get_context_data(**kwargs)

            return self.render_to_response(context=context)

custom_admin = CustomAdmin(name='custom_admin')
pawamoy commented 4 years ago

Original author: Gustavo Soeiro Senise @gustavosoeirosenise
Original date: 2016-12-29T18:56:13Z

Hello Pawamoy,
You saved me a lot of time!! But unfortunately I made something (I dont know what) that made my dashboard template stopped showing. There are no errors. It is just showing the default screen for the suit admin. I had some problems with my printer (using ubuntu) and I had to reinstall all the packages for it. I dont know if this was the cause. Also, I have packages installed in my original python3 package lib folder (/usr/local/lib/python3.5/dist-packages/), and also in other directory (/home/user/.local/lib/python3.5/site-packages). Please help!!!

pawamoy commented 4 years ago

Original date: 2016-12-30T11:06:54Z

Hi Gustavo!
Thank you for your comment :)
So if nothing is displayed, its certainly because the box you are using is raising an error, which pass silently through Django's template rendering. A solution to locate the exception would be to debug your code using something like PyCharm. Or you can try to execute each part of the code separately and manually to see what is causing the issue.

Do you work in a virtualenv? If so, could you paste the result of a "pip freeze" command?

pawamoy commented 4 years ago

Original author: Gustavo Soeiro Senise @gustavosoeirosenise
Original date: 2016-12-30T22:31:25Z

Thank you very much for your answer Pawamoy!
I am not using virtualenv for this project... maybe I should!
Here is my pip freeze:

Apache-UserALE.PyQt5==0.1.6
apt-xapian-index==0.47
apturl==0.5.2
awesome-package==1.10.3
Babel==1.3
beautifulsoup4==4.4.1
blinker==1.3
Brlapi==0.6.4
chardet==2.3.0
checkbox-support==0.22
click==6.6
command-not-found==0.3
cryptography==1.2.3
cycler==0.9.0
defer==1.0.6
devscripts===2.16.2ubuntu3
Django==1.10.4
django-admin-tools==0.8.0
django-admin-tools-stats==0.7.1
django-bower==5.2.0
django-braces==1.10.0
django-cache-utils==0.7.2
django-chartit==0.2.8
django-graphos==0.3.10
django-guardian==1.4.6
django-htmlmin==0.8.0
django-jsonfield==1.0.1
django-nvd3==0.9.7
django-qsstats-magic==0.7.2
django-suit==0.2.23
django-suit-dashboard==1.0.2
docutils==0.13.1
extras==0.0.3
feedparser==5.1.3
fixtures==2.0.0
Flask==0.11.1
Flask-Babel==0.11.1
Flask-Gravatar==0.4.2
Flask-Login==0.3.2
Flask-Mail==0.9.1
Flask-Principal==0.4.0
Flask-Security==1.7.5
Flask-SQLAlchemy==2.1
Flask-WTF==0.12
guacamole==0.9.2
gunicorn==19.6.0
html5lib==0.999
httplib2==0.9.1
idna==2.0
itsdangerous==0.24
Jinja2==2.8
language-selector==0.1
linecache2==1.0.0
louis==2.6.4
lxml==3.5.0
Magic-file-extensions==0.2
Mako==1.0.3
MarkupSafe==0.23
matplotlib==1.5.1
multicorn===-VERSION-
notify2==0.3
numpy==1.11.0
oauthlib==1.0.3
onboard==1.2.0
oneconf==0.3.9
padme==1.1.1
passlib==1.6.2
pbr==1.9.1
pexpect==4.0.1
pgadmin4==1.1
Pillow==3.1.2
piston-mini-client==0.7.5
plainbox==0.25
psutil==5.0.1
psycopg2==2.6.1
ptyprocess==0.5
pyasn1==0.1.9
pycrypto==2.6.1
pycups==1.9.73
pycurl==7.43.0
pygobject==3.20.0
PyJWT==1.3.0
pyparsing==2.0.3
PyQt5==5.7
pyrsistent==0.11.13
python-apt==1.1.0b1
python-dateutil==2.4.2
python-debian==0.1.27
python-mimeparse==1.5.1
python-nvd3==0.14.2
python-slugify==1.1.4
python-systemd==231
pytz==2014.10
pyxdg==0.25
reportlab==3.3.0
requests==2.9.1
selenium==3.0.1
sessioninstaller==0.0.0
simplejson==3.6.5
sip==4.18.1
six==1.10.0
software-center-aptd-plugins==0.0.0
speaklater==1.3
SQLAlchemy==1.0.11
sqlparse==0.1.19
system-service==0.3
testscenarios==0.5.0
testtools==2.0.0
traceback2==1.4.0
ubuntu-drivers-common==0.0.0
ufw==0.35
unattended-upgrades==0.1
Unidecode==0.4.19
unittest2==1.1.0
unity-scope-calculator==0.1
unity-scope-chromiumbookmarks==0.1
unity-scope-colourlovers==0.1
unity-scope-devhelp==0.1
unity-scope-firefoxbookmarks==0.1
unity-scope-gdrive==0.7
unity-scope-manpages==0.1
unity-scope-openclipart==0.1
unity-scope-texdoc==0.1
unity-scope-tomboy==0.1
unity-scope-virtualbox==0.1
unity-scope-yelp==0.1
unity-scope-zotero==0.1
urllib3==1.13.1
usb-creator==0.3.0
virtualenv==15.1.0
Werkzeug==0.9.6
whitenoise==3.2
WTForms==2.0.2
xdiagnose==3.8.4.1
xkit==0.0.0
XlsxWriter==0.7.3

I might buy Pycharm and test it! I am using WebStorm from the same company, for nodejs.

pawamoy commented 4 years ago

Original date: 2016-12-31T19:34:37Z

Yes you should use virtualenv to avoid mixing specific project
dependencies with system packages :) And I'm sure you can use the
Community version of PyCharm, which is free, instead of buying a
license!

Maybe we could also chat on gitter instead? My nickname there is Pawamoy also

pawamoy commented 4 years ago

Original author: Gustavo Soeiro Senise @gustavosoeirosenise
Original date: 2017-01-01T05:50:19Z

Heppy new year Pawa man! I have no batery right now on my cell. And parting a little bit. I've just bought a Pycharm license. I've just created a django app in a virtual env. Would you send to me a pip freeze for this project, so I can start from your scope? Thank you very much!!
All the best in 2017!!

pawamoy commented 4 years ago

Original author: Anupam Jain
Original date: 2017-04-19T07:38:25Z

Thanks! Your post helped with a design improvement in the structure of my Django/Highcharts code (referred to your post here: http://stackoverflow.com/qu...

pawamoy commented 4 years ago

Original date: 2017-04-19T13:47:55Z

You're welcome! Glad my post helped you :) I'm using a lot Highcharts in an automated way in my project, and I now actually pass the entire chart to the template (as JSON) from the view. Every bit of render is computed server-side, so my template works with every type of chart :) One disadvantage to my method though is that you have to hack a bit to use javascript function in your chart (tooltip.formatter, etc.).

pawamoy commented 4 years ago

Original author: Anupam Jain
Original date: 2017-04-21T09:10:53Z

Ah, thats interesting to know. Do you think it'll be cleaner if you could retain the same dynamism of creating any type of chart by using js functions and have the view only pass the actual data needed from the models? Because I am thinking its the template's responsibility to show 'how' the data is shown (what goes on x axis, what goes on y axis etc.) and the view just sends 'what' data needs to be shown. But I am still learning, so could be wrong, or it could just be a trade-off in the given use case. Happy to hear your thoughts.

pawamoy commented 4 years ago

Original date: 2017-04-21T11:12:46Z

You are absolutely right. Django stresses this in its documentation: no logic in template, no "presentation" in views. I guess you could easily "reverse" my method by using several templates (extending others or included by them), one for each type of chart, and pass only a few values to them from the views (like the series, maybe title, height, width, etc.).

I chose to compute the entire chart server-side because the project I'm working on is evolving quickly, and I didn't want to edit chart templates each time I needed to add or change a value in the chart. There are two many (an infinity?) possible combinations with Highcharts so it was easier for me to pass it all from the views, but it might not be your case!

pawamoy commented 4 years ago

Original author: Anupam Jain
Original date: 2017-04-21T11:19:09Z

Ah - got it, thanks for getting back on that!

pawamoy commented 4 years ago

Original author: Rovshan Musayev @rovshan_musayev
Original date: 2018-01-13T17:43:33Z

I have applied everything what here written but I get error ModuleNotFoundError: No module named 'admin'. What can be a problem?

pawamoy commented 4 years ago

Original date: 2018-01-22T14:58:20Z

Do you get this error when starting the server? Could you please post the entire stack trace so I have more information about this issue?

pawamoy commented 4 years ago

Original author: Rovshan Musayev @rovshan_musayev
Original date: 2018-01-22T20:51:03Z

Unfortunately, currently I haven't. After error I gave up to to implement this component

pawamoy commented 4 years ago

Original author: aymanemx @aymanemx
Original date: 2020-04-23T01:20:12Z

thank you!

pawamoy commented 4 years ago

Original date: 2020-04-23T09:11:20Z

You're welcome!