Use Python to parse HTML and integrating said script into Django

10 views
Skip to first unread message

Tonne

unread,
Apr 13, 2009, 1:24:21 AM4/13/09
to Django users
Hi

I'm not too sure how to ask this correctly (not an experienced
developer so my vocab is not very accurate), but I'll try.

I have created a Python script (collection of functions) that
generates a calendar of sorts, really just a long list of dates from a
4 year date range. It also parses the HTML, as I couldn't work out how
to do this with the limited logic available the templating language.

My question are:

Is it possible to use a "freeform" python within the Django framework,
and yet let it receive arguments from the model?

I was intially thinking of making a custom view, but then I have all
this parsed HTML and am not sure how to get it into a template.

Or should I be looking at making a custom tag?

If not the above, do you have any other pointers on how to go about
this?

Thanks

google torp

unread,
Apr 13, 2009, 2:33:45 AM4/13/09
to Django users
Hi.

It sounds like you need to change your code structure a bit to really
take advantage of Django and the possibilities it offer. If you just
want
to get your stuff working with Django and then nothing more, I guess
you could keep it like it is, but else re factoring would be a good
idea.

The way django works, you want to keep the logic separated from the
presentation. So basically that means, that you should keep the Python
code separated from your HTML. In Django terms, we use the view
to make the logic, where we can if needed, import helper functions.
When all the data has been calculated, we pass it to a template, a
html file where we with some Django code can insert data dynamically.

It sounds like you need to grasp these things, before you really get
going.

about writing views: http://docs.djangoproject.com/en/dev/topics/http/views/
about the template language http://docs.djangoproject.com/en/dev/topics/templates/
about render_to_response: http://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render-to-response

Render to response is common way of passing data to a template for
display.
Which was what your initial question was about. But I would recommend
that
you strip the html from your Python code and instead put the html in
your template
instead. That would be a more clean and Djangoish way of doing it.

~Jakob

Tonne

unread,
Apr 13, 2009, 5:17:29 AM4/13/09
to Django users
Thanks for you reply, Jakob.

I'd prefer to solve the problem the Django way. The problem for me is
that what I'm trying to do is not the usual scenario of passing the
results of query through a view to a template. I haven't found a
precedent in the docs or a tutorial elsewhere that covers this.

In my case I need to firstly update a 3 dimensional dict/list (i.e.
nested 3 levels deep - not sure if 3 dimensional is an appropriate
description), then iterate over it and put those results into an html
page. I'm probably not looking hard enough, but I can't quite seem to
grasp how to achieve this with traditional Django views and Django
templates.

TiNo

unread,
Apr 13, 2009, 7:32:16 AM4/13/09
to django...@googlegroups.com
On Mon, Apr 13, 2009 at 11:17 AM, Tonne <pascal....@gmail.com> wrote:
In my case I need to firstly update a 3 dimensional dict/list (i.e.
nested 3 levels deep - not sure if 3 dimensional is an appropriate
description), then iterate over it and put those results into an html
page. I'm probably not looking hard enough, but I can't quite seem to
grasp how to achieve this with traditional Django views and Django
templates.

Couldn't you do that with three nested for loops in the template? See:

I'd prefer to solve the problem the Django way. The problem for me is
that what I'm trying to do is not the usual scenario of passing the
results of query through a view to a template. I haven't found a
precedent in the docs or a tutorial elsewhere that covers this.

Well, what does the updating of the dict consist of? Even if it depends on the date or on other external factors, it could happen in the view. Unless updating it is only necessary once a day and takes a lot of computing power. In that case you could run it with a cronjob [1] or any other scheduled task, once a day, and save the data into a file or db.

Remember that Django can be used as what you refer to as 'freeform' python as well. You are totally free to import parts of django into an external python script, and in that way make use of django's models etc. (Only make sure to set DJANGO_SETTINGS_MODULE in your script [2])


TiNo

Tonne

unread,
Apr 13, 2009, 10:44:36 AM4/13/09
to Django users
Thanks for you input. Okay, I clearly need to do a bit of revision on
nesting loops in templates then, if you think it might be possible to
do that. 3 nested 'while' loops with incrementing variables sound
doable in a template?

>Well, what does the updating of the dict consist of?

It will need to be updated based on queryset derived from a model, so
not so much a cron job. I do have the rest of the app working as I
want it. It's the calendar with the unusual form/structure that I'm
struggling to implement with the stock Django tools.

I'll take a step back and give it another shot with logic in view and
parsing in the template.

Brian Neal

unread,
Apr 13, 2009, 11:42:54 AM4/13/09
to Django users
On Apr 13, 9:44 am, Tonne <pascal.bomp...@gmail.com> wrote:
> It's the calendar with the unusual form/structure  that I'm
> struggling to implement with the stock Django tools.
>
> I'll take a step back and give it another shot with logic in view and
> parsing in the template.

I'm not sure what you mean by "parsing"...I am assuming you mean
"generating".

But it sounds to me like you might want to look into creating your own
template tag to generate the calendar HTML. I'd rather do that than go
nuts with nested loops inside a template.

-BN

Tonne

unread,
Apr 18, 2009, 8:17:02 AM4/18/09
to Django users
> But it sounds to me like you might want to look into creating your own
> template tag to generate the calendar HTML. I'd rather do that than go
> nuts with nested loops inside a template.

Just to follow up on this...

I have a working solution. Right or wrong, my answer was to create a
template tag that generates the calendar, html and all.

As the looping I need to do is beyond the default Django template
conditionals, I patched together a multiline block type """string"""
which included the html formatting. Not very elegant, but it does the
job.

I'd still be interested to know if there would be a better way of
doing this though. As I explained, I need to build up a set of nest
lists (which forms the Calendar). In order to do that, I need to fetch
values from a 3 level deep array (combo of dicts and lists). This
necessitates that I make use while lists and iterators to find the
values.

So if anyone has a better way of doing this, please let me know and
I'll do some refactoring.
Reply all
Reply to author
Forward
0 new messages