Custom Python For Info In Sidebar

53 views
Skip to first unread message

Derek Gaston

unread,
Mar 7, 2014, 12:35:57 PM3/7/14
to mezzani...@googlegroups.com
I'm definitely new to the Django/Mezz world... but I've built some fairly complex and interesting things with Flask so I'm not completely clueless.  Actually, my Flask background may be hurting me as it seems to have an almost inverse model to Django (Flask is more "event" driven where it feels like Django is more "data" driven).

That said - I am currently absolutely stumped.  "All" I want to do is run some custom python and display the results in a box in the right sidebar (on every page).  I don't need any new Models or data or anything (the data is going to come from elsewhere on the same server).  Just a hook to run some python and present the results to the page template.

Any clues on where to start?

Thanks!

Derek

Tom Brander

unread,
Mar 7, 2014, 12:53:49 PM3/7/14
to mezzani...@googlegroups.com
Checkout the third party modlules at the bottom of this page http://mezzanine.jupo.org/docs/overview.html?highlight=third%20party Something like mezzyblocks  may be what you are after...

Derek Gaston

unread,
Mar 7, 2014, 12:54:29 PM3/7/14
to mezzani...@googlegroups.com
Ok - it appears that what I need is a "Page Processor"... but the documentation is not quite clear (to me) about where to put such a beast.  It says "inside one of your INSTALLED_APPS"... does that mean I need to create another Django App to do this custom python?  Or is there a place I can stick it right within my Mezzanine project?

Thanks for any pointers...

Derek

Derek Gaston

unread,
Mar 7, 2014, 1:04:18 PM3/7/14
to mezzani...@googlegroups.com
That does seem to be close to what I'm looking for - but it has a huge warning about not working with newer versions of Mezz... so it's probably a non-starter

I actually don't need anything very sophisticated - I can do all the HTML/template wrangling to create the block myself.  I just need to execute some custom python and send the results to the template (which I'm pretty sure is what a "Page Processor" is for)... now i just need to figure out where to put such a thing.

Thanks for the reply!

Derek

Josh Cartmell

unread,
Mar 7, 2014, 1:22:33 PM3/7/14
to mezzani...@googlegroups.com
Hey Derek, a page processor could work, although they get registered to a particular page type or slug so it wouldn't run on *all* pages.  I'm not sure what you meant about a "huge warning about not working with newer versions of Mezz", they definitely do work in the latest Mezzanine.

Another solution could be to write a custom template tag.  For example, blog_recent_posts adds a list of recent posts to the template context.  Here is the python code https://github.com/stephenmcd/mezzanine/blob/master/mezzanine/blog/templatetags/blog_tags.py#L54 and here's an example of using it in a template, https://github.com/stephenmcd/mezzanine/blob/master/mezzanine/blog/templates/blog/includes/filter_panel.html#L4

You could also use a template context processor to add stuff to the template context on every request, https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATE_CONTEXT_PROCESSORS

One or more of these solutions should get you what you want =)


--
You received this message because you are subscribed to the Google Groups "Mezzanine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mezzanine-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Derek Gaston

unread,
Mar 7, 2014, 1:47:49 PM3/7/14
to mezzani...@googlegroups.com
Thanks Josh!

A template tag looks perfect - and I'm already using that blog tag for creating a side block with blog posts in it so I can easily see how to do that.

Now: Where do I put the template tag?  All I have is a Mezzanine project - what file/directory do I need to create to put this template tag code - and how do I register it with Mezz?

Thanks for the help!

Derek

Josh Cartmell

unread,
Mar 7, 2014, 4:36:54 PM3/7/14
to mezzani...@googlegroups.com
Hey Derek, take a look at the Code Layout here, https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#code-layout

Basically you need to create a new Django app (python manage.py startapp app_name), add it to your INSTALLED_APPS, create a templatetags directory inside of the new app (don't forget to put an empty __init__.py inside the templatetags directory) and then create a file called library_name.py in that new directory (replace library_name with whatever suits you).

Derek Gaston

unread,
Mar 7, 2014, 6:21:40 PM3/7/14
to mezzani...@googlegroups.com
Awesome Josh!  Thanks so much!  I got it working!

In case someone else stumbles across this I also had to make sure that the directory containing my new app was in the Python sys.path()  (I added it in the wsgi file).  Since that directory is my Mezz directory I figured that everything would work - but I was wrong.

Thanks again!

Derek



--
You received this message because you are subscribed to a topic in the Google Groups "Mezzanine Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mezzanine-users/w0aqUq3vjBs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mezzanine-use...@googlegroups.com.

Derek Gaston

unread,
Mar 7, 2014, 7:43:00 PM3/7/14
to mezzani...@googlegroups.com
Just thought you guys might want to see the outcome:


The site is still being filled in with info (so don't judge too much!).  The part that I added today is that "Build Status" box on the right.  It's actually pulling data from my Flask based site (which is hosted on the same machine) and showing the status of our tests (which are triggered by GitHub webhooks that the Flask site responds to).

To do this - I actually just imported my Flask site into a Django app and used the Flask ORM to query the database in the template tag!  Pretty cool - and it all works seamlessly.

Eventually I'm going to rewrite that Flask site in Django so that I can integrate the two sites a little more easily - but for now this is working!

Thanks again!

Derek

Stephen McDonald

unread,
Mar 8, 2014, 2:23:14 AM3/8/14
to mezzani...@googlegroups.com
That's really cool, thanks Derek.


--
You received this message because you are subscribed to the Google Groups "Mezzanine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mezzanine-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Stephen McDonald
http://jupo.org

Josh Cartmell

unread,
Mar 10, 2014, 1:59:13 PM3/10/14
to mezzani...@googlegroups.com
Great stuff, thanks for sharing Derek and I'm glad you got it working
Reply all
Reply to author
Forward
0 new messages