Re: Post tutorial help needed - generic.create_update.create_object

2008-12-22 Thread Greg Schwimer
OK, that makes sense then. Thank you!

On Mon, Dec 22, 2008 at 5:11 PM, Malcolm Tredinnick <
malc...@pointy-stick.com> wrote:

>
> On Mon, 2008-12-22 at 15:32 -0700, Greg Schwimer wrote:
> [...]
>
> >
> > I suspect I've now moved on to a problem in a new area.  The thing I
> > don't quite get is why adding auto_now_add to the pub_date field in
> > the model broke this.   Any ideas?
>
> Because auto_now_add fields are not editable. They are automatically
> given a value at creation time.
>
> Regards,
> Malcolm
>
>
>
> >
>

--~--~-~--~~~---~--~~
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: Post tutorial help needed - generic.create_update.create_object

2008-12-22 Thread Malcolm Tredinnick

On Mon, 2008-12-22 at 15:32 -0700, Greg Schwimer wrote:
[...]

> 
> I suspect I've now moved on to a problem in a new area.  The thing I
> don't quite get is why adding auto_now_add to the pub_date field in
> the model broke this.   Any ideas?

Because auto_now_add fields are not editable. They are automatically
given a value at creation time.

Regards,
Malcolm



--~--~-~--~~~---~--~~
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: Post tutorial help needed - generic.create_update.create_object

2008-12-22 Thread Greg Schwimer
OK, that definitely fixed it, but it created another problem.  Here's how it
looks now:

*pub_date = models.DateTimeField('date published', auto_now_add=True)*

Exactly as you'd suggested.  However, I'm now getting the following error on
all pages I hit:

*ImproperlyConfigured at /polls/new/
'PollAdmin.fieldsets[1][1]['fields']' refers to field 'pub_date' that is
missing from the form.*

I got it to work by commenting out a line in my admin.py file that had some
custom stuff set up, per the tutorial.  Here's what that code looks like:

*class PollAdmin(admin.ModelAdmin):
fieldsets = [
(None,{'fields': ['question']}),
** THE OFFENDING LINE LIES HITHER:::*
*('Date Information',{'fields': ['pub_date'], 'classes':
['collapse']}),
]
inlines = [ChoiceInline]
list_display = ('question', 'last_vote_date', 'pub_date',
'was_published_today')
list_filter = ['pub_date']
search_fields = ['question']
date_hierarchy = 'pub_date'
*

I suspect I've now moved on to a problem in a new area.  The thing I don't
quite get is why adding auto_now_add to the pub_date field in the model
broke this.   Any ideas?




On Sun, Dec 21, 2008 at 9:51 PM, Malcolm Tredinnick <
malc...@pointy-stick.com> wrote:

>
> On Sun, 2008-12-21 at 21:46 -0700, Greg Schwimer wrote:
> > OK, that was very helpful, thank you.  Yes, my problem is because I am
> > not setting the date.   Thinking it through, I don't want the user
> > changing this, so it would be helpful to use the auto_now_add idea you
> > suggested.  However, I can't get that working.  Here's my mode;
> > definition for pub_date:
> >
> >  pub_date = models.DateTimeField('date published')
> >
> > The docs you referred to suggest the approach might look something
> > like this:
> >
> > pub_date = models.DateTimeField([auto_now_add=True], 'date
> > published)
>
> The square brackets in documentation are a fairly standard way of saying
> that parameter is optional. To use the parameter, type it in without the
> square brackets. Thus:
>
>pub_date = models.DateTimeField('date published', auto_now_add=True)
>
> Regards,
> Malcolm
>
>
>
> >
>

--~--~-~--~~~---~--~~
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: Post tutorial help needed - generic.create_update.create_object

2008-12-21 Thread Daniel Roseman

On Dec 22, 4:49 am, "Greg Schwimer"  wrote:
> On Sun, Dec 21, 2008 at 1:47 PM, Daniel Roseman <
>
> roseman.dan...@googlemail.com> wrote:
>
> > You won't see errors because you haven't included them in your
> > template. Either render the whole form with {{ form.as_p }} - which
> > will render the errors as well - or for each field include a reference
> > to that field's errors - {{ form.fieldname.errors }} - plus
> > {{ form.non_field_errors }} at the top of the form
>
> Yes, that showed me the errors.  Very helpful.
>
>
>
> > If you've got a non-blank field in your model that you don't want to
> > display in your form, make sure you include it in the exclude list in
> > the inner Meta class. Then set it the value manually in your view.
>
> > class MyForm(forms.ModelForm):
> >    class Meta:
> >         exclude=['pub_date']
>
> So I put this in my model directly?  Under the class definition, or?

No, it's a separate class. Put it wherever you like - it's often put
in a separate forms.py, but it just needs to be somewhere you can
import it.
BTW, I missed off a line - there should be
model=Poll
at the end, at the same indentation level as exclude.

>
> > ... in the view ...
> > if request.method=='POST':
> >    form = MyForm(request.POST)
> >    if form.is_valid()
> >        new_obj = form.save(commit=True)
> >        new_obj.pub_date=datetime.date.today()
> >        new_obj.save()
>
> OK, I get that, but I'm trying to use the generic views, so is this
> applicable to my use case?

That does make it tricky. You can pass the form in as a parameter via
the URL, but that won't help with the post-save customisation.

One possibility is to 'wrap' the generic view in a custom view. James
Bennett wrote a good blog post about this a couple of years ago:
http://www.b-list.org/weblog/2006/nov/16/django-tips-get-most-out-generic-views/
--
DR.
--~--~-~--~~~---~--~~
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: Post tutorial help needed - generic.create_update.create_object

2008-12-21 Thread Malcolm Tredinnick

On Sun, 2008-12-21 at 21:46 -0700, Greg Schwimer wrote:
> OK, that was very helpful, thank you.  Yes, my problem is because I am
> not setting the date.   Thinking it through, I don't want the user
> changing this, so it would be helpful to use the auto_now_add idea you
> suggested.  However, I can't get that working.  Here's my mode;
> definition for pub_date:
> 
>  pub_date = models.DateTimeField('date published')
> 
> The docs you referred to suggest the approach might look something
> like this:
> 
> pub_date = models.DateTimeField([auto_now_add=True], 'date
> published)

The square brackets in documentation are a fairly standard way of saying
that parameter is optional. To use the parameter, type it in without the
square brackets. Thus:

pub_date = models.DateTimeField('date published', auto_now_add=True)

Regards,
Malcolm



--~--~-~--~~~---~--~~
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: Post tutorial help needed - generic.create_update.create_object

2008-12-21 Thread Greg Schwimer
On Sun, Dec 21, 2008 at 1:47 PM, Daniel Roseman <
roseman.dan...@googlemail.com> wrote:

>
>
> You won't see errors because you haven't included them in your
> template. Either render the whole form with {{ form.as_p }} - which
> will render the errors as well - or for each field include a reference
> to that field's errors - {{ form.fieldname.errors }} - plus
> {{ form.non_field_errors }} at the top of the form


Yes, that showed me the errors.  Very helpful.


>
>
> If you've got a non-blank field in your model that you don't want to
> display in your form, make sure you include it in the exclude list in
> the inner Meta class. Then set it the value manually in your view.
>
> class MyForm(forms.ModelForm):
>class Meta:
> exclude=['pub_date']


So I put this in my model directly?  Under the class definition, or?


>
>
> ... in the view ...
> if request.method=='POST':
>form = MyForm(request.POST)
>if form.is_valid()
>new_obj = form.save(commit=True)
>new_obj.pub_date=datetime.date.today()
>new_obj.save()


OK, I get that, but I'm trying to use the generic views, so is this
applicable to my use case?

Thanks for the help!!
Greg

--~--~-~--~~~---~--~~
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: Post tutorial help needed - generic.create_update.create_object

2008-12-21 Thread Greg Schwimer
OK, that was very helpful, thank you.  Yes, my problem is because I am not
setting the date.   Thinking it through, I don't want the user changing
this, so it would be helpful to use the auto_now_add idea you suggested.
However, I can't get that working.  Here's my mode; definition for pub_date:

 pub_date = models.DateTimeField('date published')

The docs you referred to suggest the approach might look something like
this:

pub_date = models.DateTimeField([auto_now_add=True], 'date published)

or

pub_date = models.DateTimeField.auto_now_add('date published')

But neither work, nor does any variation thereof.

What am I missing?

Thanks again!
Greg


On Sun, Dec 21, 2008 at 11:34 AM, Fridrik Mar Jonsson wrote:

>
> Hi schwim,
>
> Welcome to Django!  Glad to have you. ;-)
>
> You probably receive the same page again because there were errors in
> the form, most probably because you didn't include all the fields
> required (as you suspect).  To see specifically what errors there
> were, please refer to:
>
>
> http://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#customizing-the-form-template
>
> It is usually wise to simply iterate through all fields (if you
> require a custom form layout, otherwise you can just do ``{{ form }}``
> or ``{{ form.as_* }}``) than to manually write up every field directly
> in the HTML.  This means that all fields that exist in the form will
> be exhausted and printed on the page.  Then you will only have to
> change the form to make a change to the form's HTML if you add, change
> or remove a field.
>
>
> http://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#looping-over-the-form-s-fields
>
> Note that if you want to declare a ``pub_date`` without having to
> enter the date explicitely in the form (having it default to when it
> is updated or created), use ``auto_now`` or ``auto_now_add``.
>
> http://docs.djangoproject.com/en/dev/ref/models/fields/#datefield
>
> Regards,
> Friðrik Már
>
> On Dec 21, 7:02 am, schwim  wrote:
> > OK, I'm a total newbie. I ran through the tutorial on the django site,
> > and decided to extend the poll system a bit just to learn more.  I
> > figured quite a bit out, but on this one I'm stuck.
> >
> > I want to provide a page at polls/new to add a new poll.  I'm trying
> > to use the generic.create_update.create_object generic view to provide
> > the form, which works, but when I submit the post, it doesn't follow
> > the post_save_redirect I specify.  I think I know why, but first,
> > here's my urlconf:
> >
> >  (r'^new/$', 'django.views.generic.create_update.create_object',
> > dict({'model': Poll}, post_save_redirect="../")),
> >
> > I've no idea if that is right, but I stopped getting errors with it,
> > and my template will display.  Here's the template I'm using:
> >
> >  
> >  New Poll: {{ form.question }}
> >  
> >
> > This generates the expected form. When I click submit, I get returned
> > to the same page, no errors.  I'm pretty sure this is happening
> > because I have NOT NULL in the pub_date field, and I'm trying to
> > insert a new record without a date.  Makes sense to me, but how do I
> > get there?  Like I said, I'm a total newbie to this - bash me if you
> > want. ;)
> >
> > So, what am I doing wrong, and more important to the long term, how
> > the heck do I debug something like this? I'm getting no errors.
> >
> > TIA
> > schwim
> >
>

--~--~-~--~~~---~--~~
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: Post tutorial help needed - generic.create_update.create_object

2008-12-21 Thread Daniel Roseman

On Dec 21, 7:02 am, schwim  wrote:
> OK, I'm a total newbie. I ran through the tutorial on the django site,
> and decided to extend the poll system a bit just to learn more.  I
> figured quite a bit out, but on this one I'm stuck.
>
> I want to provide a page at polls/new to add a new poll.  I'm trying
> to use the generic.create_update.create_object generic view to provide
> the form, which works, but when I submit the post, it doesn't follow
> the post_save_redirect I specify.  I think I know why, but first,
> here's my urlconf:
>
>      (r'^new/$', 'django.views.generic.create_update.create_object',
> dict({'model': Poll}, post_save_redirect="../")),
>
> I've no idea if that is right, but I stopped getting errors with it,
> and my template will display.  Here's the template I'm using:
>
>      
>      New Poll: {{ form.question }}
>      
>
> This generates the expected form. When I click submit, I get returned
> to the same page, no errors. I'm pretty sure this is happening
> because I have NOT NULL in the pub_date field, and I'm trying to
> insert a new record without a date.  Makes sense to me, but how do I
> get there?  Like I said, I'm a total newbie to this - bash me if you
> want. ;)
>
> So, what am I doing wrong, and more important to the long term, how
> the heck do I debug something like this? I'm getting no errors.


You won't see errors because you haven't included them in your
template. Either render the whole form with {{ form.as_p }} - which
will render the errors as well - or for each field include a reference
to that field's errors - {{ form.fieldname.errors }} - plus
{{ form.non_field_errors }} at the top of the form.

If you've got a non-blank field in your model that you don't want to
display in your form, make sure you include it in the exclude list in
the inner Meta class. Then set it the value manually in your view.

class MyForm(forms.ModelForm):
class Meta:
 exclude=['pub_date']

... in the view ...
if request.method=='POST':
form = MyForm(request.POST)
if form.is_valid()
new_obj = form.save(commit=True)
new_obj.pub_date=datetime.date.today()
new_obj.save()

etc.
--
DR.
--~--~-~--~~~---~--~~
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: Post tutorial help needed - generic.create_update.create_object

2008-12-21 Thread Fridrik Mar Jonsson

Hi schwim,

Welcome to Django!  Glad to have you. ;-)

You probably receive the same page again because there were errors in
the form, most probably because you didn't include all the fields
required (as you suspect).  To see specifically what errors there
were, please refer to:

http://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#customizing-the-form-template

It is usually wise to simply iterate through all fields (if you
require a custom form layout, otherwise you can just do ``{{ form }}``
or ``{{ form.as_* }}``) than to manually write up every field directly
in the HTML.  This means that all fields that exist in the form will
be exhausted and printed on the page.  Then you will only have to
change the form to make a change to the form's HTML if you add, change
or remove a field.

http://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#looping-over-the-form-s-fields

Note that if you want to declare a ``pub_date`` without having to
enter the date explicitely in the form (having it default to when it
is updated or created), use ``auto_now`` or ``auto_now_add``.

http://docs.djangoproject.com/en/dev/ref/models/fields/#datefield

Regards,
Friðrik Már

On Dec 21, 7:02 am, schwim  wrote:
> OK, I'm a total newbie. I ran through the tutorial on the django site,
> and decided to extend the poll system a bit just to learn more.  I
> figured quite a bit out, but on this one I'm stuck.
>
> I want to provide a page at polls/new to add a new poll.  I'm trying
> to use the generic.create_update.create_object generic view to provide
> the form, which works, but when I submit the post, it doesn't follow
> the post_save_redirect I specify.  I think I know why, but first,
> here's my urlconf:
>
>      (r'^new/$', 'django.views.generic.create_update.create_object',
> dict({'model': Poll}, post_save_redirect="../")),
>
> I've no idea if that is right, but I stopped getting errors with it,
> and my template will display.  Here's the template I'm using:
>
>      
>      New Poll: {{ form.question }}
>      
>
> This generates the expected form. When I click submit, I get returned
> to the same page, no errors.  I'm pretty sure this is happening
> because I have NOT NULL in the pub_date field, and I'm trying to
> insert a new record without a date.  Makes sense to me, but how do I
> get there?  Like I said, I'm a total newbie to this - bash me if you
> want. ;)
>
> So, what am I doing wrong, and more important to the long term, how
> the heck do I debug something like this? I'm getting no errors.
>
> TIA
> schwim
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Post tutorial help needed - generic.create_update.create_object

2008-12-21 Thread schwim

OK, I'm a total newbie. I ran through the tutorial on the django site,
and decided to extend the poll system a bit just to learn more.  I
figured quite a bit out, but on this one I'm stuck.

I want to provide a page at polls/new to add a new poll.  I'm trying
to use the generic.create_update.create_object generic view to provide
the form, which works, but when I submit the post, it doesn't follow
the post_save_redirect I specify.  I think I know why, but first,
here's my urlconf:

 (r'^new/$', 'django.views.generic.create_update.create_object',
dict({'model': Poll}, post_save_redirect="../")),

I've no idea if that is right, but I stopped getting errors with it,
and my template will display.  Here's the template I'm using:

 
 New Poll: {{ form.question }}
 

This generates the expected form. When I click submit, I get returned
to the same page, no errors.  I'm pretty sure this is happening
because I have NOT NULL in the pub_date field, and I'm trying to
insert a new record without a date.  Makes sense to me, but how do I
get there?  Like I said, I'm a total newbie to this - bash me if you
want. ;)

So, what am I doing wrong, and more important to the long term, how
the heck do I debug something like this? I'm getting no errors.

TIA
schwim

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