Re: Simple_tag strange behaviour
UPDATE: Thanks Anatoliy for you help. You spotted the problem and it now works as I originally wanted it to. The problem was that I had defined an array at the module level, and then updated the array with results from a queryset. Why this is a problem is that every time the page is refreshed, the array continues to be updated and grows with each update (i.e. rather than be built fresh with each update). This means that the indexes I was referencing in loops were no longer accurate and failing. The solution is to put variables used in that way inside of a function and return the array. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Simple_tag strange behaviour
Yes, that's what I suspect. I've just tested the script through a python interpretor (stripped out the django only stuff first) and it definitely works. So I'm wondering if it might be a timing issue, that somehow on a refresh, perhaps due some kind of cacheing, the queryset is not being fetched quickly enough so the script runs without it and therefore the update doesn't work? Is that even possible? On Apr 20, 6:48 pm, Anatoliy <anatoliy.la...@gmail.com> wrote: > So what is your problem? > > update_cal_links() doesn't work after page refresh? > > On Apr 20, 1:05 pm, Tonne <pascal.bomp...@gmail.com> wrote: > > > > > > I didn't see how you return array to template context. > > > Okay... well, it's actually a string that I finally send to the > > template. It's a long story (sorry), but I use the array to build a > > string (i.e values from the array get embedded in HTML source). The > > place where the string is return to the template is the last line on > > the tag, i.e. "return home_calendar". > > > The line above it "home_calendar = print_cal()", shows how > > print_calendar is a value derived from the function that builds the > > string. > > > I could show you the source of the array and string building function, > > but I'm afraid you eyes might bleed :) > > > On Apr 20, 4:37 pm, Anatoliy <anatoliy.la...@gmail.com> wrote: > > > > Tonne, > > > > I didn't see how you return array to template context. > > > > On Apr 20, 11:44 am, Tonne <pascal.bomp...@gmail.com> wrote: > > > > > Thanks > > > > > > Can you show code of your tag and template where it used? > > > > > Well, sure, but the tag is quite a big one, 150 lines long due to the > > > > large array and string concatenation going on. So I've shown here only > > > > what I assume to be the critical bits. > > > > > (btw. the tag discussed here was the solution to this > > > > issuehttp://groups.google.com/group/django-users/browse_thread/thread/ae40...) > > > > > tag > > > > -- > > > > > cal = { 2007: > > > > > { > > > > 1: [ [1], [2], #etcetera... a > > > > fairly big multidimensional > > > > array > > > > > def update_cal_links(year, month, day): > > > > """updates 'cal' array with a link to entry""" > > > > link = "/%d/%d/%d/" %(year, month, day) > > > > cal[year][month][day-1].insert(1,link) > > > > return > > > > > def print_cal(): > > > > """ Returns a string, compiling calender into a series of nested > > > > unordered lists""" > > > > #I've removed the body of this function cos it's long, ugly > > > > and it does seem to work fine > > > > > @register.simple_tag > > > > def do_home_calendar(): > > > > """ Populates calendar with links to entries, > > > > returns a formatted calendar to the template.""" > > > > entries = Entry.objects.all() > > > > for date in entries: > > > > link = date.splitDate() #model method > > > > update_cal_links(int(link['year']), int(link['month']), > > > > int(link > > > > ['day'])) # function expects integers > > > > home_calendar = print_cal() > > > > return home_calendar > > > > > template > > > > > > > > > {% extends "base.html" %} > > > > {% load simple_tags %} > > > > > {% block content %} > > > > {% do_home_calendar %} > > > > {% if archive_list %} > > > > hello > > > > > > > > {% for entry in archive_list %} > > > > {{ entry.date }} > > > li> > > > > {% endfor %} > > > > > > > > {% else %} > > > > FAIL! > > > > {% endif %} > > > > {% endblock %} --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Simple_tag strange behaviour
> I didn't see how you return array to template context. Okay... well, it's actually a string that I finally send to the template. It's a long story (sorry), but I use the array to build a string (i.e values from the array get embedded in HTML source). The place where the string is return to the template is the last line on the tag, i.e. "return home_calendar". The line above it "home_calendar = print_cal()", shows how print_calendar is a value derived from the function that builds the string. I could show you the source of the array and string building function, but I'm afraid you eyes might bleed :) On Apr 20, 4:37 pm, Anatoliy <anatoliy.la...@gmail.com> wrote: > Tonne, > > I didn't see how you return array to template context. > > On Apr 20, 11:44 am, Tonne <pascal.bomp...@gmail.com> wrote: > > > > > Thanks > > > > Can you show code of your tag and template where it used? > > > Well, sure, but the tag is quite a big one, 150 lines long due to the > > large array and string concatenation going on. So I've shown here only > > what I assume to be the critical bits. > > > (btw. the tag discussed here was the solution to this > > issuehttp://groups.google.com/group/django-users/browse_thread/thread/ae40...) > > > tag > > -- > > > cal = { 2007: > > > { > > 1: [ [1], [2], #etcetera... a fairly > > big multidimensional > > array > > > def update_cal_links(year, month, day): > > """updates 'cal' array with a link to entry""" > > link = "/%d/%d/%d/" %(year, month, day) > > cal[year][month][day-1].insert(1,link) > > return > > > def print_cal(): > > """ Returns a string, compiling calender into a series of nested > > unordered lists""" > > #I've removed the body of this function cos it's long, ugly > > and it does seem to work fine > > > @register.simple_tag > > def do_home_calendar(): > > """ Populates calendar with links to entries, > > returns a formatted calendar to the template.""" > > entries = Entry.objects.all() > > for date in entries: > > link = date.splitDate() #model method > > update_cal_links(int(link['year']), int(link['month']), > > int(link > > ['day'])) # function expects integers > > home_calendar = print_cal() > > return home_calendar > > > template > > > > > {% extends "base.html" %} > > {% load simple_tags %} > > > {% block content %} > > {% do_home_calendar %} > > {% if archive_list %} > > hello > > > > {% for entry in archive_list %} > > {{ entry.date }} > li> > > {% endfor %} > > > > {% else %} > > FAIL! > > {% endif %} > > {% endblock %} --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Simple_tag strange behaviour
Thanks > Can you show code of your tag and template where it used? Well, sure, but the tag is quite a big one, 150 lines long due to the large array and string concatenation going on. So I've shown here only what I assume to be the critical bits. (btw. the tag discussed here was the solution to this issue http://groups.google.com/group/django-users/browse_thread/thread/ae40745aca946cf8/7b536034daa92e2b?lnk=gst=tonne#7b536034daa92e2b) tag -- cal = { 2007: { 1: [ [1], [2], #etcetera... a fairly big multidimensional array def update_cal_links(year, month, day): """updates 'cal' array with a link to entry""" link = "/%d/%d/%d/" %(year, month, day) cal[year][month][day-1].insert(1,link) return def print_cal(): """ Returns a string, compiling calender into a series of nested unordered lists""" #I've removed the body of this function cos it's long, ugly and it does seem to work fine @register.simple_tag def do_home_calendar(): """ Populates calendar with links to entries, returns a formatted calendar to the template.""" entries = Entry.objects.all() for date in entries: link = date.splitDate() #model method update_cal_links(int(link['year']), int(link['month']), int(link ['day'])) # function expects integers home_calendar = print_cal() return home_calendar template {% extends "base.html" %} {% load simple_tags %} {% block content %} {% do_home_calendar %} {% if archive_list %} hello {% for entry in archive_list %} {{ entry.date }} {% endfor %} {% else %} FAIL! {% endif %} {% endblock %} --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Simple_tag strange behaviour
I have a simple_tag that receives a queryset, then updates an array based on the queryset result (using a function), and finally it returns the array to the template context. It works beautifully the first time it is viewed with a browser. However, on subsequent "pageviews" the array is not update, and an "untouched" array is sent in the context. If I resave the simple_tag source file, it will again work perfectly for the first view, while subsequent views will do as above. So clearly the first time the server receives a HTTP request it the script runs correctly, but somehow either the queryset is not being received of the function is not being called after first request. This is while running on the development server by the way. I'm a bit stumped. Anyone have an idea of that this could be? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Use Python to parse HTML and integrating said script into Django
> 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. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Use Python to parse HTML and integrating said script into Django
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. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Use Python to parse HTML and integrating said script into Django
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. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Use Python to parse HTML and integrating said script into Django
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 --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Preserving pretty HTML output
Good point, thanks very much, but I think I'll just live with the way it currently works. What I've done is to take a bit more care how and where I place template tags, and this has improved things somewhat. On Dec 4, 1:25 am, adelevie <[EMAIL PROTECTED]> wrote: > You may also want to look at BeautifulSoup. It is an html parser > writter for python. It has a method called soup.prettify() in which > "soup" is a string of html. prettify() outputs cleanly formatted html. > Approximation: > soup = "titlehello world" > soup.pretiffy()>>> > > > title > hello world > > > > I hope this helps. > > On Dec 3, 3:00 am, Tonne <[EMAIL PROTECTED]> wrote: > > > > > Thank you for the detailed response, Malcolm. I wasn't aware of the > > complexities of the issue and understand better now why it is the way > > it is. It was something that was really bugging me, but I feel like I > > can let it go now :) > > > I'm not skilled enough in Python to take a crack at solving the > > problem myself. Although I'd prefer my HTML source output to look > > good, I'll take readability of template code (and performance) over > > rendered output prettiness. > > > Thanks again. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Preserving pretty HTML output
Thank you for the detailed response, Malcolm. I wasn't aware of the complexities of the issue and understand better now why it is the way it is. It was something that was really bugging me, but I feel like I can let it go now :) I'm not skilled enough in Python to take a crack at solving the problem myself. Although I'd prefer my HTML source output to look good, I'll take readability of template code (and performance) over rendered output prettiness. Thanks again. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Preserving pretty HTML output
I am interested to see if anyone could share their solutions for ensuring pretty HTML output. I have found achieving it to be a very uncomfortable compromise in that I seem to need to make my templates almost unreadable to do so, which isn't really a practical solution. Perhaps nicely formatted HTML markup is simply not a worthy priority in the greater scheme of things Django. Which is a bit of a pity, as I've always appreciated well-manicured HTML, and it has been a point of professional pride to make my own handwritten HTML as readable as possible. And older thread brought up this subject too, but no conclusive solutions were brought to light... http://groups.google.com/group/django-users/browse_thread/thread/c9e569f7370c9c80/f11e4c24d3517233?lnk=gst=output+whitespace#f11e4c24d3517233 --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Basic SQL
I must have read that section ten times, but not seen the wood for the trees in my haste to find the right approach. It's making more sense to me now. Thanks again. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Basic SQL
Daniel, thank you (x100)! That was exactly what I was looking for. > {% for entry in entries %} > {{ entry.title }} > {% for image in entry.image_set.all %} > > {% endfor %} > {% endfor %} Now, could I ask what the name of the concept is that allows "image_set" to spontaneously exist, so that I can read up more about it in the docs? I seemed to have missed it so far. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Basic SQL
I think I may be barking up the wrong tree, and if so please excuse the above. I now suspect that I should be associating the images with their relevant entries with template tags. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Basic SQL
*disclosure* I am SQL novice, okay */disclosure* I have what I think is a very simple problem, but can't seem to find a solution without writing raw SQL. I have 2 (simplified here) models: class Entry(models.Model): title = models.CharField(max_length=80) class Image(models.Model): image = models.ImageField(upload_to="img/") entry = models.ForeignKey(Entry) Each entry can have many images. I would like to build a query that returns all the available Entries and Images related to each Entry. My view looks like this... from xxx.xxx.models import Entry from xxx.xxx.models import Image def index(request): entry_list = Entry.objects.all() image_list = Image.objects.select_related('entry') Currently, I am getting the entire list of images with each Entry, rather than only the specific images related to an Entry (if you know what I mean). To illustrate, my results resemble this: Entry 1 + Image 1, Image 2, Image 3 Entry 2 + Image 1, Image 2, Image 3 Entry 3 + Image 1, Image 2, Image 3 whereas I would like to see Entry 1 + Image 1 Entry 2 + Image 2 Entry 3 + Image 3 I hope this make sense. If anyone would care to show me how to build the right query, I'd be very grateful. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Query subset
In this case, as the amount of data is fairly small, I'm inclined to download the full queryset and let the template ignore the irrelevant data. Thanks Russ & Marcelo for your very helpful insights. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Query subset
Thanks Marcelo. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Query subset
So, what I've done is use Model.objects.values() to limit the returned values, which is not ideal as I'm losing the objectness of the queryset. I've worked around the loss Queryset.get_absolute_url by using a less than elegant semi-hardcoded url. So if I'm missing a blindingly obvious way of limiting a query to nominated fields so that I can still keep the benefits of a queryset, please let me know. Also, is using Model.objects.values() to limit the fields returned actually more efficient than Model.objects.all() in terms of database hits? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Query subset
I have spent hours looking in the docs and one this list for an answer to this problem: I have a model that has, for example, 20 fields. On my site's homepage, where I'd like to offer a preview version of the object, I'd need to retrieve only say, half of those fields to be displayed. My dilemma is whether to simply use a Model.objects.all() query and ignore the unnecessary data in the template, or find a more efficient way to call only the data I need. Perhaps my model design is flawed and I should separate out the data that is not common to the list and detail version of the content, or perhaps I should have a model method that returns only the data fields I need, etc. Forgive me, as I'm very new to this, but I'm stumped. Any suggestions would be gratefully received. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Admin & ForeignKey confusion
Thanks very much Karen. You are right. I have gotten confused by the inline option, and it is not required in the solution I was looking for. I have got it working now, --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Admin & ForeignKey confusion
n00b alert, btw: --- I'm very new to django so please forgive the stupid questions, but I'm struggling to get some very basic model relations to work. I'd be very grateful if someone could show me where I am going wrong. I have 2 incredibly simple models, where a Project would have one of many Medium types. So when in the admin form, I would like a project to have an option list of Mediums to choose from.. (from work.models.py) from django.db import models class Medium(models.Model): medium = models.CharField(max_length=20) class Project(models.Model): title = models.CharField(max_length=80) medium = models.ForeignKey(Medium) ...this validates and SQL looks as it should. However, when I try to implement the Admin (from work.admin.py) from portfolio.work.models import Project from portfolio.work.models import Medium from django.contrib import admin class MediumInline(admin.StackedInline): """docstring for ClassName""" model = Medium extra = 3 class ProjectAdmin(admin.ModelAdmin): inlines = [MediumInline] admin.site.register(Project, ProjectAdmin) select I get an exception: has no ForeignKey to Have I gotten my data modelling fundamentally incorrect, or am I doing something wrong in admin.py --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---