<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Perplexed Labs - Latest Comments in Adventures in Django and Python &amp;#8211; Part III</title><link>http://plabs.disqus.com/</link><description>web development war stories from the frontlines to the backend</description><atom:link href="https://plabs.disqus.com/adventures_in_django_and_python_8211_part_iii/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Mon, 17 Aug 2009 17:14:05 -0000</lastBuildDate><item><title>Re: Adventures in Django and Python &amp;#8211; Part III</title><link>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/#comment-14979707</link><description>&lt;p&gt;You could do something crazy like subclass the form and give it special processing that will munge some of the fields in ways you approve of.  We did that back on 0.96 to use the automatic model form fields but have some of them hidden (there was no "uneditable" then) or to be able to set the entire form to be uneditable.&lt;/p&gt;&lt;p&gt;In the process I hacked up the automatic form generation a fair bit - unfortunately not all the required pieces were exposed by newforms at the time, which was very irritating since I don't expect to have python modules have functions I'm not allowed to get to somehow.  I expect many of the specifics have changed but the basic idea might still work, though not cleanly.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Andrew Shultz</dc:creator><pubDate>Mon, 17 Aug 2009 17:14:05 -0000</pubDate></item><item><title>Re: Adventures in Django and Python &amp;#8211; Part III</title><link>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/#comment-14839718</link><description>&lt;p&gt;In addition to letting you easily generate HTML4 from your forms (slash, begone!), the "field" template tag provided by &lt;a href="http://github.com/simonw/django-html/" rel="nofollow noopener" target="_blank" title="http://github.com/simonw/django-html/"&gt;http://github.com/simonw/dj...&lt;/a&gt; lets you specify attributes for rendered form fields:&lt;/p&gt;&lt;p&gt;{% field form.somefield class="special" %}&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jonathan Buchanan</dc:creator><pubDate>Fri, 14 Aug 2009 12:49:05 -0000</pubDate></item><item><title>Re: Adventures in Django and Python &amp;#8211; Part III</title><link>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/#comment-14832089</link><description>&lt;p&gt;You have to define those attributes somewhere.  It makes more sense (to me) to do that within the template than in your form definition or in your view.&lt;/p&gt;&lt;p&gt;Even if you ultimately style the attributes using CSS you have to specify that class somewhere.  I'd like to able to easily do that from within the template.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Reiferson</dc:creator><pubDate>Fri, 14 Aug 2009 09:46:40 -0000</pubDate></item><item><title>Re: Adventures in Django and Python &amp;#8211; Part III</title><link>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/#comment-14825953</link><description>&lt;p&gt;Shouldn't those attributes be defined in CSS? Then it's not quite so unappealing to simply use 'attrs' in your form definition to modify or append to the class name (even this might not be needed - you can use the id assigned to each form element or just use CSS attribute selectors).&lt;/p&gt;&lt;p&gt;Anyway the point is you say styling doesn't belong in Python and I would argue it doesn't belong in the template either.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">andybak</dc:creator><pubDate>Fri, 14 Aug 2009 05:19:13 -0000</pubDate></item><item><title>Re: Adventures in Django and Python &amp;#8211; Part III</title><link>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/#comment-14794914</link><description>&lt;p&gt;DISQUS supports some html, apparently not PRE :(&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Reiferson</dc:creator><pubDate>Thu, 13 Aug 2009 15:06:13 -0000</pubDate></item><item><title>Re: Adventures in Django and Python &amp;#8211; Part III</title><link>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/#comment-14794644</link><description>&lt;p&gt;A quick look at BoundField and it does not appear the chaining of attrs would work.  Maybe a comma: (how do you make this thing keep formatting?)&lt;/p&gt;&lt;p&gt;@register.filter&lt;br&gt;def with_attrs(value, arg):&lt;br&gt;    attrs = {}&lt;br&gt;    for x in arg.split(','):&lt;br&gt;        v = x.strip().split('=')&lt;br&gt;        attrs[v[0]] = v[1]&lt;/p&gt;&lt;p&gt;    return value.as_widget(attrs=attrs)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">John</dc:creator><pubDate>Thu, 13 Aug 2009 15:00:55 -0000</pubDate></item><item><title>Re: Adventures in Django and Python &amp;#8211; Part III</title><link>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/#comment-14793261</link><description>&lt;p&gt;Yea,  like i said this had been floating around for a while..  Space was just the first thing that jumped out at me.  Of course that does not work for style stuff.&lt;/p&gt;&lt;p&gt;might be better to write it so it can be chained:&lt;/p&gt;&lt;p&gt;{{ &lt;a href="http://form.name" rel="nofollow noopener" target="_blank" title="form.name"&gt;form.name&lt;/a&gt;|with_attrs:"size=10"|with_attrs:"style=width: 720px" }}&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">John</dc:creator><pubDate>Thu, 13 Aug 2009 14:28:05 -0000</pubDate></item><item><title>Re: Adventures in Django and Python &amp;#8211; Part III</title><link>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/#comment-14792968</link><description>&lt;p&gt;I looked up the 'attrs' documentation, which is sparse.  I don't think it's the responsibility of the view or the Form definition to define template related attributes like style, size, etc.  I think these attributes should elegantly be modified within the template.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Reiferson</dc:creator><pubDate>Thu, 13 Aug 2009 14:21:17 -0000</pubDate></item><item><title>Re: Adventures in Django and Python &amp;#8211; Part III</title><link>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/#comment-14792903</link><description>&lt;p&gt;John, this is exactly in line with what I'd like to be able to accomplish.  I like where you're going with this.&lt;/p&gt;&lt;p&gt;Perhaps splitting on something other than a space?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Reiferson</dc:creator><pubDate>Thu, 13 Aug 2009 14:20:07 -0000</pubDate></item><item><title>Re: Adventures in Django and Python &amp;#8211; Part III</title><link>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/#comment-14792375</link><description>&lt;p&gt;I've oft wondered what the best way to set attrs in the template would be,  somewhere in the back of my mind this filter base solution has been floating around.  5 minutes and a real quick test shows it has promise.&lt;/p&gt;&lt;p&gt;@register.filter&lt;br&gt;def with_attrs(value, arg):&lt;br&gt;    args = arg.split(' ')&lt;br&gt;    attrs = {}&lt;br&gt;    for v in args:&lt;br&gt;        v1 = v.split('=')&lt;br&gt;        attrs[v1[0]] = v1[1]&lt;/p&gt;&lt;p&gt;    return value.as_widget(attrs=attrs)&lt;/p&gt;&lt;p&gt;{{ &lt;a href="http://form.name" rel="nofollow noopener" target="_blank" title="form.name"&gt;form.name&lt;/a&gt;|with_attrs:"maxlength=10" }}&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">John</dc:creator><pubDate>Thu, 13 Aug 2009 14:09:16 -0000</pubDate></item><item><title>Re: Adventures in Django and Python &amp;#8211; Part III</title><link>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/#comment-14790291</link><description>&lt;p&gt;Check out the `attrs` kwarg in form fields. Something like:&lt;/p&gt;&lt;p&gt;    title = forms.CharField(attrs={'class': 'myclass'})&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jökull Sólberg Auðunsson</dc:creator><pubDate>Thu, 13 Aug 2009 13:17:59 -0000</pubDate></item><item><title>Re: Adventures in Django and Python &amp;#8211; Part III</title><link>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/#comment-14786036</link><description>&lt;p&gt;Interesting, thanks.&lt;/p&gt;&lt;p&gt;I suppose using the 2nd method you proposed, count(), would be ideal for performance/memory reasons because you're not transferring the result-set from the database.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Reiferson</dc:creator><pubDate>Thu, 13 Aug 2009 11:57:29 -0000</pubDate></item><item><title>Re: Adventures in Django and Python &amp;#8211; Part III</title><link>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/#comment-14785672</link><description>&lt;p&gt;If don't plan on using the result of your filter statement, instead of:&lt;br&gt;    if account.useraccount_set.filter(user__exact=request.user) != []:&lt;br&gt;you could try:&lt;br&gt;    if account.useraccount_set.filter(user__exact=request.user).count()&lt;br&gt;or if you do need the result:&lt;br&gt;    try:&lt;br&gt;        user_account = account.useraccount_set.get(user=request.user)&lt;br&gt;        ...&lt;br&gt;    except UserAccount.DoesNotExist:&lt;br&gt;        ....&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">megaman821</dc:creator><pubDate>Thu, 13 Aug 2009 11:50:03 -0000</pubDate></item><item><title>Re: Adventures in Django and Python &amp;#8211; Part III</title><link>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/#comment-14784184</link><description>&lt;p&gt;Thanks Jake.  As usually is the case it's fantastic for a large number of scenarios but, as you dig deeper and need to finely tune things, you begin to identify potential areas for improvement.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Reiferson</dc:creator><pubDate>Thu, 13 Aug 2009 11:20:04 -0000</pubDate></item><item><title>Re: Adventures in Django and Python &amp;#8211; Part III</title><link>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/#comment-14784030</link><description>&lt;p&gt;Thanks for pointing that out, I'll update the post.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Reiferson</dc:creator><pubDate>Thu, 13 Aug 2009 11:17:26 -0000</pubDate></item><item><title>Re: Adventures in Django and Python &amp;#8211; Part III</title><link>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/#comment-14783633</link><description>&lt;p&gt;Great post Matt. While I do appreciate the advantages of the forms module over having to do it all manually, I agree that I have to fight with it a lot. Hopefully it'll be one of the areas that gets improved as Django evolves.&lt;/p&gt;&lt;p&gt;I just wanted to let you know, Python (2.5 and up I believe) does in fact have a ternary operator; it's just a slightly different syntax. In your example, I could go:&lt;/p&gt;&lt;p&gt;label = "true" if booleanVariable else "false"&lt;/p&gt;&lt;p&gt;Unfortunately, it's fairly recent, so if you use it and then end up on an old version (&amp;lt; 2.5 I believe), you'll be in trouble.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jake Boxer</dc:creator><pubDate>Thu, 13 Aug 2009 11:10:09 -0000</pubDate></item><item><title>Re: Adventures in Django and Python &amp;#8211; Part III</title><link>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/#comment-14782262</link><description>&lt;p&gt;There is a ternary operator in python:&lt;br&gt;value_when_true if condition else value_when_false&lt;br&gt;3 if a==2 else 101&lt;br&gt;introduced in v2.5.&lt;/p&gt;&lt;p&gt;Using the and-or hack you will run into problems, it's not recommended&lt;br&gt;[] and 'true' or false &lt;br&gt;is false for example.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">attila</dc:creator><pubDate>Thu, 13 Aug 2009 10:41:53 -0000</pubDate></item></channel></rss>