Re: call to render_to_response with RequestContext within a Generic View causes DatabaseError when resolving the user.is_authenticated template variable

2013-09-06 Thread giovanni allegri
I don't know what is happening but I've solved with a workaround:

dummy = self.request.user.is_authenticated()

calling is_authenticated before returning render_to_response solves the 
problem, because request.user is "casted" to the actual user instance, 
instead of remaining a SimpleLazyObject.

I hope to understand why it happens one day...

giovanni

Il giorno venerdì 6 settembre 2013 16:10:22 UTC+2, giovanni allegri ha 
scritto:
>
> The problem seems to be related with SimpleLazyObject, wrapping the 
> auth.user.
> If during debugging I access the function, everything works fine. It's 
> enough to have the Variables windows open in PyDev.
> If I do not access it, it isn't resolved to the actual user instance (in 
> my case AnonymousUser), and then the template variable lookup fails. I 
> don't know exactly when, but it fails...
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Re: call to render_to_response with RequestContext within a Generic View causes DatabaseError when resolving the user.is_authenticated template variable

2013-09-06 Thread giovanni allegri
The problem seems to be related with SimpleLazyObject, wrapping the 
auth.user.
If during debugging I access the function, everything works fine. It's 
enough to have the Variables windows open in PyDev.
If I do not access it, it isn't resolved to the actual user instance (in my 
case AnonymousUser), and then the template variable lookup fails. I don't 
know exactly when, but it fails...


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


call to render_to_response with RequestContext within a Generic View causes DatabaseError when resolving the user.is_authenticated template variable

2013-09-06 Thread giovanni allegri
I don't know if I'm following a wrong pattern in the context of Django. 
Inside my class views I need manage exceptions and I would like to pass the 
response workflow to an external method (my exceptions module). In my call 
I pass my ClassView.request variable to be able to use request context 
processors.
I paste an excerpt, where I do the render_to_response call directly from 
the form_valid method, just to test it.

class ProjectCreateView(FormView):
"""Create view."""
(...)

def form_valid(self, form):
"""Form and logic behind project creation."""

project_file = form.cleaned_data['project_file']
thumbnail = form.cleaned_data['thumbnail']
description = form.cleaned_data['description']

try:

except IntegrityError as e:
data = {'error':{'msg':_('project_overwrite_error')}}
return 
render_to_response('generic_error.html',data,context_instance=RequestContext(self.request))
except Exception as e:
return HttpResponse(e)

return HttpResponseRedirect(self.group.get_absolute_url())

(...)


Passing the RequestContext instance causes a DatabaseError, raised when it 
tries to test the user.is_authenticated template context variable.

I've noticed that when used in a basic view method, the resolution of 
user.is_authenticated is called only once, while it's hit twice in the case 
of the generic view. I don't know if it can be the source of the error...

Another strange thing is that it doesn't happen if I debug with a 
breakpoint inside the view dispathc method. Wired!

Here it is the stacktrace: https://gist.github.com/anonymous/6460412

First, I would like to understand why this happens.
Second, how should Itransfer the response flow outside a generic view? It 
works fine until I don't ask for the user variable, so I suppose there 
should be a cleaner way to manage the control tranfer.

Would you help me to solve the issue?
Thanks a lot,
Giovanni

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Re: RequestContext and user.is_authenticated confusion

2010-02-14 Thread Kev Dwyer
On Sun, 14 Feb 2010 08:30:05 -0800, Achim Domma wrote:

> Hi Kev,
> 
> thanks for the hint. Trying different solutions I introduced indeed a
> typo. Now I can see the username and is_authenticated works as expected.
> But still curious: Is there a good reason, why I have to pass
> RequestContext each time? Whenever I'm using Django and having the
> feeling to violate DRY, there's usually a better solution. I'm quite
> surprised that it's not the case this time!?
> 
> cheers,
> Achim

Hello Achim,

I'm glad it's working for you now.

I don't know the original reason for having to use RequestContext; there 
is a hint in bug #650 that authentication wasn't thought to be a
common enough use case to justify the processing overhead of making
RequestContext available by default.

The idea of adding RequestContext to render_to_request by default was 
considered for 1.1 but special-cased, apparently because consensus could
not be reached.  Search the Django Developers list if you want the details.

All the best,

Kev


-- 
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: RequestContext and user.is_authenticated confusion

2010-02-14 Thread Achim Domma
Hi Kev,

thanks for the hint. Trying different solutions I introduced indeed a
typo. Now I can see the username and is_authenticated works as
expected. But still curious: Is there a good reason, why I have to
pass RequestContext each time? Whenever I'm using Django and having
the feeling to violate DRY, there's usually a better solution. I'm
quite surprised that it's not the case this time!?

cheers,
Achim

-- 
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: RequestContext and user.is_authenticated confusion

2010-02-14 Thread andreas schmid
Kev Dwyer wrote:
> On Sun, 14 Feb 2010 01:19:41 -0800, Achim Domma wrote:
>
>   
>> Hi,
>>
>> depending on if a user is logged in or not, I want to display a "login"
>> form or a "logout" button. I tried to use this code snippet:
>>
>> http://docs.djangoproject.com/en/1.1/topics/auth/#id6
>>
>> If I render my view using render_to_response("myTemplate.html") the user
>> variable is empty. If I use
>> render_to_response("myTemplate.html",context_instance=RequestContext(request))
>> I get the correct user and can display the username. But
>> is_authenticated still always returns false!? Any hint what I might be
>> doing wrong?
>>
>> According to the documentation
>> "context_instance=RequestContext(request)" should only be required when
>> passing a additional data dictionary to render_to_response. As passing
>> data to a view and checking if the user is logged in should be something
>> quite common, this sounds strange to me!?
>>
>> I want to display the user information / status in my master template,
>> so having to pass a RequestContext into the template from each view
>> would be very tedious. Feels like I'm on a complete wrong way!?
>>
>> Any hint would be very appreciated!
>>
>> cheers,
>> Achim
>> 
>
> Hello Achim,
>
> It's difficult to tell why is_authenticated returns false for you, but
> assuming there aren't any typos in your template then you need to check 
> that your views are working correctly, particularly that authentication
> are not "lost" by forgetting to pass them to one of your views.
>
> Passing a RequestContext to your templates is the correct way to give your 
> templates access to authentication information.  It seems cumbersome
> while your changing all your render_to_response calls but it doesn't
> need any maintenance after that.
>
> Cheers,
>
> Kev
>
>   
did you put 'django.core.context_processors.request' into your
TEMPLATE_CONTEXT_PROCESSORS in your settings.py?

-- 
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: RequestContext and user.is_authenticated confusion

2010-02-14 Thread Kev Dwyer
On Sun, 14 Feb 2010 01:19:41 -0800, Achim Domma wrote:

> Hi,
> 
> depending on if a user is logged in or not, I want to display a "login"
> form or a "logout" button. I tried to use this code snippet:
> 
> http://docs.djangoproject.com/en/1.1/topics/auth/#id6
> 
> If I render my view using render_to_response("myTemplate.html") the user
> variable is empty. If I use
> render_to_response("myTemplate.html",context_instance=RequestContext(request))
> I get the correct user and can display the username. But
> is_authenticated still always returns false!? Any hint what I might be
> doing wrong?
> 
> According to the documentation
> "context_instance=RequestContext(request)" should only be required when
> passing a additional data dictionary to render_to_response. As passing
> data to a view and checking if the user is logged in should be something
> quite common, this sounds strange to me!?
> 
> I want to display the user information / status in my master template,
> so having to pass a RequestContext into the template from each view
> would be very tedious. Feels like I'm on a complete wrong way!?
> 
> Any hint would be very appreciated!
> 
> cheers,
> Achim

Hello Achim,

It's difficult to tell why is_authenticated returns false for you, but
assuming there aren't any typos in your template then you need to check 
that your views are working correctly, particularly that authentication
are not "lost" by forgetting to pass them to one of your views.

Passing a RequestContext to your templates is the correct way to give your 
templates access to authentication information.  It seems cumbersome
while your changing all your render_to_response calls but it doesn't
need any maintenance after that.

Cheers,

Kev

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



RequestContext and user.is_authenticated confusion

2010-02-14 Thread Achim Domma
Hi,

depending on if a user is logged in or not, I want to display a
"login" form or a "logout" button. I tried to use this code snippet:

http://docs.djangoproject.com/en/1.1/topics/auth/#id6

If I render my view using render_to_response("myTemplate.html") the
user variable is empty. If I use
render_to_response("myTemplate.html",context_instance=RequestContext(request))
I get the correct user and can display the username. But
is_authenticated still always returns false!? Any hint what I might be
doing wrong?

According to the documentation
"context_instance=RequestContext(request)" should only be required
when passing a additional data dictionary to render_to_response. As
passing data to a view and checking if the user is logged in should be
something quite common, this sounds strange to me!?

I want to display the user information / status in my master template,
so having to pass a RequestContext into the template from each view
would be very tedious. Feels like I'm on a complete wrong way!?

Any hint would be very appreciated!

cheers,
Achim

-- 
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: user.is_authenticated

2009-06-02 Thread Jarek Zgoda

Wiadomość napisana w dniu 2009-06-01, o godz. 18:50, przez Tim Sawyer:

>
> You need a RequestContext for the user object to be available in  
> templates
>
> http://lincolnloop.com/blog/2008/may/10/getting-requestcontext-your-templates/
>
> I use a render_auth method instead of render_to_response, which  
> automatically
> adds the RequestContext to all of my templates.
>
> Tim.
>
> On Monday 01 June 2009 17:40:53 K.Berkhout wrote:
>> Hi,
>>
>> Is there a way I can access the "user.is_authenticated" method in
>> every view, without having to manually pass the User model to every
>> template? Basicly I want to show a login or logout link on every  
>> page,
>> depending on wether the visitor is logged in or not. I've included  
>> the
>> following if statement in my base template:
>>
>> {% if user.is_authenticated %}
>>   Welcome {{ user }} , showing logout link...
>> {% else %}
>>   Showing login link...
>> {% endif %}
>>
>> However, it only works with the standard "accounts/login" view, as
>> that view has acces to the user.is_authenticated method.
>>

For completeness sake: RequestContext *and*  
django.core.context_processors.auth in TEMPLATE_CONTEXT_PROCESSORS  
(it's there by default unless one changes configuration).

-- 
Artificial intelligence stands no chance against natural stupidity

Jarek Zgoda, R&D, Redefine
jarek.zg...@redefine.pl


--~--~-~--~~~---~--~~
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: user.is_authenticated

2009-06-01 Thread K.Berkhout

Thank you very much for your quick replies, I didn't know about the
RequestContext!
Everything works like a charm now!

Kevin

On 1 jun, 18:48, Genis Pujol Hamelink  wrote:
> Hi,
>
> The variable you want to use should be in every page's default context.
>
> #views.py
> from django.template import RequestContext
>
> def myview(request):
>
>     myvar = foo
>     variables = RequestContext(request, { 'myvar' : myvar })
>     return render_to_response("template.html", variables)
>
> In variables you will have all the variables from the default contexts plus
> the ones you include in the dictionary... the auth context is loaded by
> default (if i remember correctly)...
>
> hope it helps,
>
> g
>
> 2009/6/1 K.Berkhout 
>
>
>
>
>
> > Hi,
>
> > Is there a way I can access the "user.is_authenticated" method in
> > every view, without having to manually pass the User model to every
> > template? Basicly I want to show a login or logout link on every page,
> > depending on wether the visitor is logged in or not. I've included the
> > following if statement in my base template:
>
> > {% if user.is_authenticated %}
> >   Welcome {{ user }} , showing logout link...
> > {% else %}
> >   Showing login link...
> > {% endif %}
>
> > However, it only works with the standard "accounts/login" view, as
> > that view has acces to the user.is_authenticated method.
>
> > Thanks,
>
> > Kevin
>
> --
> Genís
--~--~-~--~~~---~--~~
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: user.is_authenticated

2009-06-01 Thread Genis Pujol Hamelink
Hi,

The variable you want to use should be in every page's default context.

#views.py
from django.template import RequestContext

def myview(request):

myvar = foo
variables = RequestContext(request, { 'myvar' : myvar })
return render_to_response("template.html", variables)

In variables you will have all the variables from the default contexts plus
the ones you include in the dictionary... the auth context is loaded by
default (if i remember correctly)...

hope it helps,

g



2009/6/1 K.Berkhout 

>
> Hi,
>
> Is there a way I can access the "user.is_authenticated" method in
> every view, without having to manually pass the User model to every
> template? Basicly I want to show a login or logout link on every page,
> depending on wether the visitor is logged in or not. I've included the
> following if statement in my base template:
>
> {% if user.is_authenticated %}
>   Welcome {{ user }} , showing logout link...
> {% else %}
>   Showing login link...
> {% endif %}
>
> However, it only works with the standard "accounts/login" view, as
> that view has acces to the user.is_authenticated method.
>
> Thanks,
>
> Kevin
> >
>


-- 
Genís

--~--~-~--~~~---~--~~
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: user.is_authenticated

2009-06-01 Thread Tim Sawyer

You need a RequestContext for the user object to be available in templates

http://lincolnloop.com/blog/2008/may/10/getting-requestcontext-your-templates/

I use a render_auth method instead of render_to_response, which automatically 
adds the RequestContext to all of my templates.

Tim.

On Monday 01 June 2009 17:40:53 K.Berkhout wrote:
> Hi,
>
> Is there a way I can access the "user.is_authenticated" method in
> every view, without having to manually pass the User model to every
> template? Basicly I want to show a login or logout link on every page,
> depending on wether the visitor is logged in or not. I've included the
> following if statement in my base template:
>
> {% if user.is_authenticated %}
>Welcome {{ user }} , showing logout link...
> {% else %}
>Showing login link...
> {% endif %}
>
> However, it only works with the standard "accounts/login" view, as
> that view has acces to the user.is_authenticated method.
>
> Thanks,
>
> Kevin
> 

--~--~-~--~~~---~--~~
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: user.is_authenticated

2009-06-01 Thread Masklinn

On 1 juin 09, at 18:40, "K.Berkhout"  wrote:
> Hi,
>
> Is there a way I can access the "user.is_authenticated" method in
> every view, without having to manually pass the User model to every
> template? Basicly I want to show a login or logout link on every page,
> depending on wether the visitor is logged in or not. I've included the
> following if statement in my base template:
>
> {% if user.is_authenticated %}
>   Welcome {{ user }} , showing logout link...
> {% else %}
>   Showing login link...
> {% endif %}
>
> However, it only works with the standard "accounts/login" view, as
> that view has acces to the user.is_authenticated method.
>
> Thanks,
>
> Kevin
>
Look up RequestContext. 

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



user.is_authenticated

2009-06-01 Thread K.Berkhout

Hi,

Is there a way I can access the "user.is_authenticated" method in
every view, without having to manually pass the User model to every
template? Basicly I want to show a login or logout link on every page,
depending on wether the visitor is logged in or not. I've included the
following if statement in my base template:

{% if user.is_authenticated %}
   Welcome {{ user }} , showing logout link...
{% else %}
   Showing login link...
{% endif %}

However, it only works with the standard "accounts/login" view, as
that view has acces to the user.is_authenticated method.

Thanks,

Kevin
--~--~-~--~~~---~--~~
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: user.is_authenticated is always false using default login

2008-05-20 Thread James Bennett

On Tue, May 20, 2008 at 10:32 AM, Andrew English
<[EMAIL PROTECTED]> wrote:
> Do I need to explicitly call authenticate and login in my own view to
> populate the user data?  From what I read, it seems that the
> django.contrib.auth.views.login does that automatically.

There's a difference between authenticating the user, which you've
done, and making the user object available as a variable to the
template, which I'm guessing you haven't done. You probably want to
read this:

http://www.djangoproject.com/documentation/templates_python/#subclassing-context-requestcontext


-- 
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

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



user.is_authenticated is always false using default login

2008-05-20 Thread Andrew English
My view function uses the login required decorator:

@login_required(redirect_field_name='next')
def index(request):
   lists = List.objects.filter(user=request.user)
   return render_to_response('base.html', {'lists': lists})

This directs them to the login page:
LOGIN_URL = '/todone/list/login/'
LOGIN_REDIRECT_URL = '/todone/list/'

Which is correctly handled by the django auth default:
(r'^todone/list/login/$', 'django.contrib.auth.views.login'),

I am using the default template from the authentication instructions:

{% extends "base.html" %}

{% block content %}

{% if form.has_errors %}
Your username and password didn't match. Please try again.
{% endif %}



Username:{{ form.username
}}
Password:{{ form.password
}}






{% endblock %}

When the user logins in with correct credentials, they are redirected to the
expected page.  However in the template, user.is_authenticated is false and
user.username is empty.  But in the views.py, lists =
List.objects.filter(user=request.user) will return the expected results.
Even though the user is not authenticated, the request.user object has the
expected values.

Do I need to explicitly call authenticate and login in my own view to
populate the user data?  From what I read, it seems that the
django.contrib.auth.views.login does that automatically.

Thanks.

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