Re: How to choose one row of data

2009-12-15 Thread Tom Evans
On Fri, Dec 11, 2009 at 6:25 PM, Andy  wrote:
> Tom - DR's method is simple and effective, but I'm guessing you say
> it's the worst way because it creates an unnecessary database
> request.  Is this a correct assumption?  If not, please explain.
>
>

There are at least 2 things wrong with it IMO:

1) Not using django's URL conf to generate the redirect URL. This
means that URL is hardcoded and duplicated.
2) Storing state unnecessarily in the session, ie, storing the current
order in the session. Once the order is created, we have a single
unambiguous URL to the order, that will not change. If you store it in
the session, you can only refer to that order by having the 'right
session'.

Cheers

Tom

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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: How to choose one row of data

2009-12-11 Thread TiNo
Why wouldn't you fetch the latest (completed) order by that particular user
from the db? No id passing around, and it should give you only one result.

If you pass an id, and don't use the session, make sure the order belongs to
the user, otherwise users could see other users orders based on id
guessing...

Tino

On Fri, Dec 11, 2009 at 19:25, Andy  wrote:

> Tom - DR's method is simple and effective, but I'm guessing you say
> it's the worst way because it creates an unnecessary database
> request.  Is this a correct assumption?  If not, please explain.
>
>
> On Dec 11, 10:58 am, Tom Evans  wrote:
> > On Fri, Dec 11, 2009 at 4:44 PM, Andy  wrote:
> > > Thank you DR.  For other newbies out there I changed my views to this
> > > and it worked great:
> >
> > > articles = Context({'articles': Articles.objects.all()})
> >
> > > def order(request):
> > >if request.method == 'POST':
> > >form = OrderForm(request.POST)
> > >if form.is_valid():
> > >current_order = form.save()
> > >order_info = Context({'order_info':
> current_order})
> > >request.session['order_info'] = order_info
> > >return HttpResponseRedirect('/order_complete/')
> > >else:
> > >form = OrderForm()
> >
> > >return render_to_response('order.html', {'form': form},
> articles)
> >
> > > def order_complete(request):
> > >order_info = request.session['order_info']
> > >return render_to_response('order_complete.html', order_info,
> > > articles)
> >
> > This is the worst way to pass state. The state should be passed via
> > the user, rather than storing it in the session. Your view should look
> > something like this:
> >
> > def order(request):
> >   if request.method == 'POST':
> > form = OrderForm(request.POST)
> > if form.is_valid():
> >   order = form.save()
> >   return HttpResponseRedirect(reverse('order_complete', args=[
> order.id]))
> >   else:
> > form = OrderForm()
> >
> >   return render_to_response('order.html', {'form': form}, articles)
> >
> > def order_complete(request, order_id=None):
> >   order = get_object_or_404(Order, id=order_id)
> >   return render_to_response('order_complete.html', {'order': order},
> articles)
> >
> > Cheers
> >
> > Tom
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@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.
>
>
>

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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: How to choose one row of data

2009-12-11 Thread Andy
Tom - DR's method is simple and effective, but I'm guessing you say
it's the worst way because it creates an unnecessary database
request.  Is this a correct assumption?  If not, please explain.


On Dec 11, 10:58 am, Tom Evans  wrote:
> On Fri, Dec 11, 2009 at 4:44 PM, Andy  wrote:
> > Thank you DR.  For other newbies out there I changed my views to this
> > and it worked great:
>
> > articles = Context({'articles': Articles.objects.all()})
>
> > def order(request):
> >        if request.method == 'POST':
> >                form = OrderForm(request.POST)
> >                if form.is_valid():
> >                        current_order = form.save()
> >                        order_info = Context({'order_info': current_order})
> >                        request.session['order_info'] = order_info
> >                        return HttpResponseRedirect('/order_complete/')
> >        else:
> >                form = OrderForm()
>
> >        return render_to_response('order.html', {'form': form}, articles)
>
> > def order_complete(request):
> >        order_info = request.session['order_info']
> >        return render_to_response('order_complete.html', order_info,
> > articles)
>
> This is the worst way to pass state. The state should be passed via
> the user, rather than storing it in the session. Your view should look
> something like this:
>
> def order(request):
>   if request.method == 'POST':
>     form = OrderForm(request.POST)
>     if form.is_valid():
>       order = form.save()
>       return HttpResponseRedirect(reverse('order_complete', args=[order.id]))
>   else:
>     form = OrderForm()
>
>   return render_to_response('order.html', {'form': form}, articles)
>
> def order_complete(request, order_id=None):
>   order = get_object_or_404(Order, id=order_id)
>   return render_to_response('order_complete.html', {'order': order}, articles)
>
> Cheers
>
> Tom

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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: How to choose one row of data

2009-12-11 Thread Tom Evans
On Fri, Dec 11, 2009 at 4:44 PM, Andy  wrote:
> Thank you DR.  For other newbies out there I changed my views to this
> and it worked great:
>
> articles = Context({'articles': Articles.objects.all()})
>
> def order(request):
>        if request.method == 'POST':
>                form = OrderForm(request.POST)
>                if form.is_valid():
>                        current_order = form.save()
>                        order_info = Context({'order_info': current_order})
>                        request.session['order_info'] = order_info
>                        return HttpResponseRedirect('/order_complete/')
>        else:
>                form = OrderForm()
>
>        return render_to_response('order.html', {'form': form}, articles)
>
> def order_complete(request):
>        order_info = request.session['order_info']
>        return render_to_response('order_complete.html', order_info,
> articles)

This is the worst way to pass state. The state should be passed via
the user, rather than storing it in the session. Your view should look
something like this:

def order(request):
  if request.method == 'POST':
form = OrderForm(request.POST)
if form.is_valid():
  order = form.save()
  return HttpResponseRedirect(reverse('order_complete', args=[order.id]))
  else:
form = OrderForm()

  return render_to_response('order.html', {'form': form}, articles)

def order_complete(request, order_id=None):
  order = get_object_or_404(Order, id=order_id)
  return render_to_response('order_complete.html', {'order': order}, articles)

Cheers

Tom

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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: How to choose one row of data

2009-12-11 Thread Andy
Thank you DR.  For other newbies out there I changed my views to this
and it worked great:

articles = Context({'articles': Articles.objects.all()})

def order(request):
if request.method == 'POST':
form = OrderForm(request.POST)
if form.is_valid():
current_order = form.save()
order_info = Context({'order_info': current_order})
request.session['order_info'] = order_info
return HttpResponseRedirect('/order_complete/')
else:
form = OrderForm()

return render_to_response('order.html', {'form': form}, articles)

def order_complete(request):
order_info = request.session['order_info']
return render_to_response('order_complete.html', order_info,
articles)

On Dec 11, 9:30 am, Daniel Roseman  wrote:
> On Dec 11, 3:13 pm, Andy  wrote:
>
>
>
>
>
> > Thanks for the response Shawn.  I have done what you suggested,
> > assigning a variable named order_info and can use it if I use
> > render_to_response in my order view.  But I want to use
> > HttpResponseRedirect after the form is saved.  I'm thinking I need to
> > get the order_info variable into my order_complete view.  How can I do
> > this?
>
> > articles = Context({'articles': Articles.objects.all()})
>
> > def order(request):
> >         if request.method == 'POST':
> >                 form = OrderForm(request.POST)
> >                 if form.is_valid():
> >                         current_order = form.save()
> >                         order_info = Context({'order_info': current_order})
> >                         return render_to_response('order_complete.html', 
> > order_info,
> > articles) #this works
> >                         return HttpResponseRedirect('/order_complete/') 
> > #but this is what I
> > want to use
> >         else:
> >                 form = OrderForm()
>
> >         return render_to_response('order.html', {'form': form}, articles)
>
> > def order_complete(request): #how can I put order_info here?
> >         return render_to_response('order_complete.html', order_info,
> > articles)
>
> The order_complete view needs some way of knowing about the object
> you've just created. Obviously, it's in the database, so all you need
> to give that view is some way to identify it - eg by the id field.
>
> So you'll need to either pass it as a URL parameter to the
> order_complete view:
>     return HttpResponseRedirect('/order_complete/%s/' %
> current_order.id)
>
> or put it into the session when you save the form, and get it out
> again in the order_complete view. Obviously if there's any personal
> info involved, you should not use the first method, or at least add
> some extra authentication.
> --
> 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-us...@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: How to choose one row of data

2009-12-11 Thread Tom Evans
On Fri, Dec 11, 2009 at 3:13 PM, Andy  wrote:
> Thanks for the response Shawn.  I have done what you suggested,
> assigning a variable named order_info and can use it if I use
> render_to_response in my order view.  But I want to use
> HttpResponseRedirect after the form is saved.  I'm thinking I need to
> get the order_info variable into my order_complete view.  How can I do
> this?
>
> articles = Context({'articles': Articles.objects.all()})
>
> def order(request):
>        if request.method == 'POST':
>                form = OrderForm(request.POST)
>                if form.is_valid():
>                        current_order = form.save()
>                        order_info = Context({'order_info': current_order})
>                        return render_to_response('order_complete.html', 
> order_info,
> articles) #this works
>                        return HttpResponseRedirect('/order_complete/') #but 
> this is what I
> want to use
>        else:
>                form = OrderForm()
>
>        return render_to_response('order.html', {'form': form}, articles)
>
> def order_complete(request): #how can I put order_info here?
>        return render_to_response('order_complete.html', order_info,
> articles)
>

You should create a named view for your order_complete view, that
takes the id of your current order, and then redirect to that instead.

Create a named view with an argument:
http://docs.djangoproject.com/en/dev/topics/http/urls/#named-groups

Get a url to a named view with an argument:
http://docs.djangoproject.com/en/dev/topics/http/urls/#django.core.urlresolvers.reverse

Cheers

Tom

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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: How to choose one row of data

2009-12-11 Thread Daniel Roseman
On Dec 11, 3:13 pm, Andy  wrote:
> Thanks for the response Shawn.  I have done what you suggested,
> assigning a variable named order_info and can use it if I use
> render_to_response in my order view.  But I want to use
> HttpResponseRedirect after the form is saved.  I'm thinking I need to
> get the order_info variable into my order_complete view.  How can I do
> this?
>
> articles = Context({'articles': Articles.objects.all()})
>
> def order(request):
>         if request.method == 'POST':
>                 form = OrderForm(request.POST)
>                 if form.is_valid():
>                         current_order = form.save()
>                         order_info = Context({'order_info': current_order})
>                         return render_to_response('order_complete.html', 
> order_info,
> articles) #this works
>                         return HttpResponseRedirect('/order_complete/') #but 
> this is what I
> want to use
>         else:
>                 form = OrderForm()
>
>         return render_to_response('order.html', {'form': form}, articles)
>
> def order_complete(request): #how can I put order_info here?
>         return render_to_response('order_complete.html', order_info,
> articles)
>

The order_complete view needs some way of knowing about the object
you've just created. Obviously, it's in the database, so all you need
to give that view is some way to identify it - eg by the id field.

So you'll need to either pass it as a URL parameter to the
order_complete view:
return HttpResponseRedirect('/order_complete/%s/' %
current_order.id)

or put it into the session when you save the form, and get it out
again in the order_complete view. Obviously if there's any personal
info involved, you should not use the first method, or at least add
some extra authentication.
--
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-us...@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: How to choose one row of data

2009-12-11 Thread Andy
Thanks for the response Shawn.  I have done what you suggested,
assigning a variable named order_info and can use it if I use
render_to_response in my order view.  But I want to use
HttpResponseRedirect after the form is saved.  I'm thinking I need to
get the order_info variable into my order_complete view.  How can I do
this?

articles = Context({'articles': Articles.objects.all()})

def order(request):
if request.method == 'POST':
form = OrderForm(request.POST)
if form.is_valid():
current_order = form.save()
order_info = Context({'order_info': current_order})
return render_to_response('order_complete.html', 
order_info,
articles) #this works
return HttpResponseRedirect('/order_complete/') #but 
this is what I
want to use
else:
form = OrderForm()

return render_to_response('order.html', {'form': form}, articles)

def order_complete(request): #how can I put order_info here?
return render_to_response('order_complete.html', order_info,
articles)

On Dec 9, 8:10 pm, Shawn Milochik  wrote:
> If you assign the output of the save() method, you'll get the created object.
>
> Instead of:
>
> form.save()
>
> do:
>
> current_order = form.save()
>
> Then you can do whatever you like with current_order, which will be an 
> instance of whatever the class 'meta' is of your OrderForm, assuming that 
> OrderForm is a forms.ModelForm.
>
> Shawn

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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: How to choose one row of data

2009-12-09 Thread Shawn Milochik
If you assign the output of the save() method, you'll get the created object.

Instead of:

form.save()

do:

current_order = form.save()

Then you can do whatever you like with current_order, which will be an instance 
of whatever the class 'meta' is of your OrderForm, assuming that OrderForm is a 
forms.ModelForm.

Shawn


--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.




How to choose one row of data

2009-12-09 Thread Andy
This is a very beginner question:

Say a user has just filled out a form and hit the submit button.  They
are redirected to an order confirmation page.  How can I retrieve the
one row of data the user just submitted and display it on the order
confirmation page?

Here is my simple View so far, but it currently displays all the user
inputed data, not the specific row the user just entered:

articles = Context({'articles': Articles.objects.all()})
order_info = Context({'order_data': Order_Info.objects.all()})

def order(request):
if request.method == 'POST':
form = OrderForm(request.POST)
if form.is_valid():
form.save()

return HttpResponseRedirect('/order_complete/')
else:
form = OrderForm()

return render_to_response('order.html', {'form': form}, articles)

def order_complete(request):
return render_to_response('order_complete.html', order_info,
articles)

Thanks for your help!

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.