Re: Best way to pass data to a HttpResponseRedirect?

2007-07-27 Thread Michael Lake

Nis Jørgensen wrote:
> As others have said, you can stuff things into request.session (after
> adding the right middleware incantations).

Using the session middleware looks far better but it will take me a while to 
try that 
out. I'll try and do that next week.

> What is so bad about leaving the url in the browser? I assume the error
> message you display is the "correct" response for that url. Just
> remember to add the correct status code - probably 400,403 or 404. Note
> that if this is a GET url, it shouldn't have side effects, so your
> "main?delete=100" example seems like a bad idea to begin with (unless
> this is the page that shows an "Are you sure ...?" message). If it is a
> POST, there is a lot of reason not to redirect it, since this will make
> it harder for the user to use the back button to fix things.

Yes its a GET that changes the database and I should look at that and change
"main?delete=100" to main/del/100/ or something and make it a POST.
And yes if I make it a POST I hadn't thought about users using the back button 
- 
which the will do :-(

Thanks for this advice, and to Patrick for how he does things.

Mike




--~--~-~--~~~---~--~~
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: Best way to pass data to a HttpResponseRedirect?

2007-07-27 Thread Nis Jørgensen

Michael Lake skrev:
> Hi all
>
> Nis Jørgensen wrote:
>   
>> The argument to HttpResponseRedirect is a url. You seem to be confusing
>> it with a template. 
>> 
>
> OK I can do this:
>
>   code 
>   # some error occurs
>   message = 'You have ... tell admin that '
>   return HttpResponseRedirect('error/')
>
> and have in views.py
>
> def error(request, message):
> {
>return render_to_response('error_page.html', {'message':message})
> }
>
> But how to get the message into error() without passing it as a GET?
>   
As others have said, you can stuff things into request.session (after
adding the right middleware incantations).

>> But if you have the data available, there is no reason to do a redirect.
>> Just render the error message etc to the relevant template, then return
>> that to the user.
>> 
>
> Why I dont want to pass it like this ?message='You have ... tell admin that 
> '
> is that its long and if the error is something like main?delete=100 but the 
> user cant 
> delete that id then a Redirect goes to a nice clean valid URL.
> A render_to_response leaves the incorrect URL in the browser.
What is so bad about leaving the url in the browser? I assume the error
message you display is the "correct" response for that url. Just
remember to add the correct status code - probably 400,403 or 404. Note
that if this is a GET url, it shouldn't have side effects, so your
"main?delete=100" example seems like a bad idea to begin with (unless
this is the page that shows an "Are you sure ...?" message). If it is a
POST, there is a lot of reason not to redirect it, since this will make
it harder for the user to use the back button to fix things.

Nis Jorgensen




--~--~-~--~~~---~--~~
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: Best way to pass data to a HttpResponseRedirect?

2007-07-27 Thread patrick k.

we are using a context_processor.

e.g., the view for a login-page could look like this:

if everythins is ok:
request.session['messages'] = ['message', 'You are logged in.']
return HttpResponseRedirect(referer)
else:
 messages = ['error', 'Some error message.']

and then, you can use a context_processor:

TEMPLATE_CONTEXT_PROCESSORS = (
 
 'www.views.context_processors.get_messages',
)

get_messages could look like this (get_messages checks the session  
and returns a message-dict):

def get_messages(request):

 messages = []
 if request.session.get('messages'):
 messages = [request.session['messages'][0], request.session 
['messages'][1]]
 del request.session['messages']

 return {
 'messages': messages,
 }

patrick


Am 27.07.2007 um 08:57 schrieb Michael Lake:

>
> Hi all
>
> Nis Jørgensen wrote:
>> The argument to HttpResponseRedirect is a url. You seem to be  
>> confusing
>> it with a template.
>
> OK I can do this:
>
>   code 
>   # some error occurs
>   message = 'You have ... tell admin that '
>   return HttpResponseRedirect('error/')
>
> and have in views.py
>
> def error(request, message):
> {
>return render_to_response('error_page.html', {'message':message})
> }
>
> But how to get the message into error() without passing it as a GET?
>
>> If you want to display different content, you need
>> to pass a different url or (not recommended) store the data you  
>> want to
>> display, then display it to the user at the new url
>
>> But if you have the data available, there is no reason to do a  
>> redirect.
>> Just render the error message etc to the relevant template, then  
>> return
>> that to the user.
>
> Why I dont want to pass it like this ?message='You have ... tell  
> admin that '
> is that its long and if the error is something like main?delete=100  
> but the user cant
> delete that id then a Redirect goes to a nice clean valid URL.
> A render_to_response leaves the incorrect URL in the browser.
>
> Mike
> -- 
>
>
>
> >


--~--~-~--~~~---~--~~
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: Best way to pass data to a HttpResponseRedirect?

2007-07-27 Thread Michael Lake


Ah does this way seem sensible?

> Nis Jørgensen wrote:
>>The argument to HttpResponseRedirect is a url. You seem to be confusing
>>it with a template. 

code 
# some error occurs
return HttpResponseRedirect('error/2/')


def error(request, message):
{
 error_messages = {
'1': 'You dont have access.' ,
'2': 'Server error ...'
 }  
 return render_to_response('error_page.html', {'message':message})
}

That way the user gets a nice clean URL and I can send a specific message into 
the 
template.

Is there some commonly accepted practice here in Django?

Mike





--~--~-~--~~~---~--~~
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: Best way to pass data to a HttpResponseRedirect?

2007-07-27 Thread Michael Lake

Hi all

Nis Jørgensen wrote:
> The argument to HttpResponseRedirect is a url. You seem to be confusing
> it with a template. 

OK I can do this:

code 
# some error occurs
message = 'You have ... tell admin that '
return HttpResponseRedirect('error/')

and have in views.py

def error(request, message):
{
   return render_to_response('error_page.html', {'message':message})
}

But how to get the message into error() without passing it as a GET?

> If you want to display different content, you need
> to pass a different url or (not recommended) store the data you want to
> display, then display it to the user at the new url

> But if you have the data available, there is no reason to do a redirect.
> Just render the error message etc to the relevant template, then return
> that to the user.

Why I dont want to pass it like this ?message='You have ... tell admin that 
'
is that its long and if the error is something like main?delete=100 but the 
user cant 
delete that id then a Redirect goes to a nice clean valid URL.
A render_to_response leaves the incorrect URL in the browser.

Mike
-- 



--~--~-~--~~~---~--~~
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: Best way to pass data to a HttpResponseRedirect?

2007-07-27 Thread Nis Jørgensen

Michael Lake skrev:
> Hi all
>
> A really simple question:
>
> I'm mostly using code like this:
>   data = {'message': 'Some message to user...'}
>   return render_to_response('main.html', data)
>
> But for error messages to users if one wishes to use a redirect like this:
>   return HttpResponseRedirect('/error_page.html')
>
> How can one add data into this page? The Redirect is nice in that it does not 
> show 
> the old URL which might be quite wrong - hence the error.
>
> The Django docs say that HttpResponseRedirect('/whatever/url/') takes just 
> one 
> argument. I'd like to use one error_page.html template for a range of errors.
>
> Do people use maybe HttpResponseRedirect('error/') where error is some 
> defined 
> function and somehow pass a message string to it?
The argument to HttpResponseRedirect is a url. You seem to be confusing
it with a template. If you want to display different content, you need
to pass a different url or (not recommended) store the data you want to
display, then display it to the user at the new url

But if you have the data available, there is no reason to do a redirect.
Just render the error message etc to the relevant template, then return
that to the user.

Nis Jorgensen

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



Best way to pass data to a HttpResponseRedirect?

2007-07-27 Thread Michael Lake

Hi all

A really simple question:

I'm mostly using code like this:
data = {'message': 'Some message to user...'}
return render_to_response('main.html', data)

But for error messages to users if one wishes to use a redirect like this:
return HttpResponseRedirect('/error_page.html')

How can one add data into this page? The Redirect is nice in that it does not 
show 
the old URL which might be quite wrong - hence the error.

The Django docs say that HttpResponseRedirect('/whatever/url/') takes just one 
argument. I'd like to use one error_page.html template for a range of errors.

Do people use maybe HttpResponseRedirect('error/') where error is some defined 
function and somehow pass a message string to it?

Mike





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