views:

1197

answers:

2

Weird problem in Django with forms :

I have a Form.class defined like this ...

class MeetingForm(forms.Form):
    owner = forms.ModelChoiceField(
            queryset=Profile.objects.all(),
            widget=forms.HiddenInput() )
    date = forms.DateTimeField()
    name = forms.CharField(max_length=30)
    etc.

And I create new instances by calling a function like this ...

def newMeetingForm(request,profile) :
    mf = MeetingForm( {
        'date' : date.today(),        
        } )
    return mf

Which works as expected. Except, the "owner"-field of the meeting is blank. What I think I should do is pre-fill the owner field in the newMeetingForm like this :

def newMeetingForm(request,profile) :
    mf = MeetingForm( {
        'date' : date.today(),
        'owner' : profile,
        } )
    return mf

where the argument "profile" is a Profile object.

When I run this, however, the form doesn't render.

I'm using this in the template :

{{ form.as_p }}

But with that extra 'owner' line in newMeetingForm it literally seems to come out as an empty string. I assume that somehow it's failing silently, perhaps as the ModelChoiceField tries to render the owner value that I prefilled it with. But the values look OK. So what else should I be looking for?

A: 

Are you sure you are passing an actual instance of a Profile to newMeetingForm?

TokenMacGuy
I'm pretty sure ... it came out of " request.user.get_profile() "
interstar
+2  A: 

Your code looks correct, largely speaking. I actually ran a "stub" project on my machine using the form and form-creation code you have, with successful results (admittedly, I guessed at what a Profile model might look like).

One thing to consider, though it won't affect rendering, is that you should be instantiating the MeetingForm with the dictionary as initial data. If you pass it as the first positional argument, django will assume you mean for that data to be the POST data, and that therefore the form should be validated. (Apologies if you already know this and where just short-cutting for the purposes of posing the question).

I often find that django views swallow important errors. To test your form try doing it in the Django shell:

$ python manage.py shell
>>> from blah.models import Profile
>>> from blah.forms import MeetingForm
>>> from datetime import date
>>> p = Profile.objects.get(id=someid)
>>> mf = MeetingForm(initial={'date':date.today(), 'owner':p})
>>> print mf.as_p()

This will output the form and, if there are serious explosions, the errors as well.

Also, and I know this is silly but I've done stuff like this too :-) (extra set of eyes... etc...), are you sure you're passing the 'form' variable to the context for your template rendering? And that the variable is called 'form', not 'mf' or something like that?

Hope that helps

Jarret Hardie
yes ... that was it (the "initial=") . Thank you VERY much. Been struggling with that for a while.
interstar
I didn't know about the validation of the first argument in this context
interstar
Cool! Glad I could help. Gotta love those empty strings in the templates :-)
Jarret Hardie