Re: how do change TextField size on form?

2007-04-05 Thread BrandonC

The newforms module isn't tied to the models, but you can easily
create a form from a model using the helper function from_from_model.

So in your view:

from django import newforms
from models import YourModel

def some_view(request):
  form_class = newforms.form_from_model(YourModel)
  #Modify the auto-built form class here.
  form_class.base_fields['Address'].widget =
newforms.widgets.Textarea(attrs={'rows':3, 'cols':40}))

  if request.POST:
form = form_class(request.POST)
if form.is_valid():
  #Do form save/post stuff here
  else:
form = form_class()

  return render_to_response('some_template', {'form':form})

I imagine this may change in the future as newforms is further
improved upon, but it works well this way currently.

Brandon

On Apr 5, 1:13 pm, "Frank" <[EMAIL PROTECTED]> wrote:
> Thanks,
>
> I am not at that level yet. I still am going through the tutorial.
>
> So do I not use the admin interface, but create my own forms?
>
> Regards,
>
> Frank
>
> - Original Message -
> From: "gordyt" <[EMAIL PROTECTED]>
> To: "Django users" 
> Sent: Thursday, April 05, 2007 4:05 PM
> Subject: Re: how do change TextField size on form?
>
> > Hi Frank,
>
> > I'm using the newforms stuff and it lets you do exactly what you
> > want.  Here is one of my simpler forms:
>
> > class AddEditCustomerForm(forms.Form):
> >name = forms.CharField(max_length=200)
> >customer_type_id = forms.ChoiceField(label="Customer Type")
> >notes = forms.CharField(widget=forms.Textarea(attrs={'rows':
> > '3'}),required=False)
> >def __init__(self, data=None):
> >super(AddEditCustomerForm,self).__init__(data)
> >self.fields['customer_type_id'] =
> > forms.ChoiceField(label="Customer Type",
> >choices=[(c.id,c.description) for c in
> > CustomerType.objects.all()])
>
> > See how I specified the widget for the "notes" CharField?  The attrs
> > argument to Textarea will let you specify how the HTML for the form is
> > generated.  In your case you would use something like this:
>
> > widget=forms.Textarea(attrs={'rows' : '3', 'cols' : '40'},...)
>
> > --gordy


--~--~-~--~~~---~--~~
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: Adding an error to an empty error list with newforms?

2007-04-05 Thread BrandonC

Oliver,

form['fieldname'] returns a Field class object.  To add an error to
that you want to do form.errors['fieldname'] = ['Your list of errors
messages'].

If you also want to preserve other possible errors you'll want to us
form.errors.setdefault('fieldname', []).append('Your error here.')
instead. The setdefault is because errors is a dictionary of lists,
but if it is empty you have to initialize it with an empty list for
that field, but if it is not you want to append to the errors list.

I've also used this method to create general errors that are displayed
somewhere else without using the standard form errors display.  For
example, if you do form.errors['general'] = ['Some general error']
then you can use form.errors.general to display those errors in your
template.

There may be better/cleaner ways to tie into the error messages
mechanism for newforms, but I tried both ways, raising ValidationError
in an overridden clean function, or just adding to the errors lists
and it seemed easier/cleaner to do just add to the errors dictionary.

If you want to preserve the as_ul and as_p formatting that is expected
in some templates for the errors you'll want to use "from
django.newforms.util import ErrorList" and replace where I've used a
list with ErrorList. Now you can call in your template,
forms.errors.fieldname.as_ul etc.

Brandon

On Apr 4, 11:10 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> Thanks for your comments. I've had a dig around in the code for
> newforms -- and it appears that to make an error, it raises
> django.newforms.util.ValidationError -- the only thing I don't get is
> how it binds this to a form field -- any ideas as all it seems to take
> is one argument -- the error message...?


--~--~-~--~~~---~--~~
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: Django IDE

2007-04-05 Thread BrandonC

As a comparison I have to say that Wing IDE has a very nice Python
debugger (fast and it works).  I haven't used Komodo in a while but I
remember my biggest issue with it was that its debugger stopped at
exceptions that truely were handled, and that it was slow.

For django I set my manage.py file as the main debug file, and then
add --noreload --settings=devsettings (my settings for running a test
server) in the debug run arguments and I can run the dev. server
locally to debug.

There may be better editors I don't know, but I find that I'm always
comparing them to what I can do in Wing.

Brandon


--~--~-~--~~~---~--~~
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: Help with Django base template and CSS

2006-03-04 Thread BrandonC

(r'^media/(?P.*)$', 'django.views.static.serve',
{'document_root': 'path/to/document/root/media', 'show_indexes':
True}),

As a temporary measure you can add a line like this to your urls.py in
order to serve media when developing, replacing the path line with an
absolute path to your media.  Ideally is should be done through your
web server though.

See: http://www.djangoproject.com/documentation/static_files/


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---