So  much code .....
Ok,  lets start

2012/4/4 Matheus Ashton <matheusash...@gmail.com>:
> urls.py:
>
> urlpatterns = patterns('',
>     url(r'^/login$', LoginView.as_view()),
>     url(r'^$', HomeView.as_view(), {}, 'home'),
> )
>
More simple way  is using decorator for checking  if user is authorized.

Add next string to your  urls.py

# import decorator functionality
from django.contrib.auth.decorators import login_required

#  import your views

from home.views import HomeView

 final  urls.py   is :


from django.conf.urls.defaults import patterns, include, url
from django.contrib.auth.decorators import login_required
from home.views import HomeView

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
    (r'^login/', 'django.contrib.auth.views.login', {'template_name':
'login.html'}),
    (r'^$', login_required(HomeView.as_view())),
)

 First line is mean  that default djano auth  mechanism  is used for
checking user's auth.
Socond line means : each request is passed to  HomeView but   should
be  authorized. If  user is not authorized  then  redirect to login
windows should be .


As result  your views.py should be :

from django.views.generic import TemplateView
# Create your views here.
class HomeView(TemplateView):
    template_name='home.html'

 Only three lines of the code !
You dont  need more!


 Thanks,
 Serge



> views.py:
>
> class LoginView(FormView):
>   form_class = LoginForm
>   template_name = "auth/form.html"
>
>   def post(self, request, *args, **kwargs):
>     form = self.get_form(self.get_form_class())
>     if form.is_valid():
>       user = self.request['login']
>       password = self.request['password']
>       try:
>         auth = Authenticator()
>         auth.authenticate(user, password, self.request)
>         return self.form_valid(form)
>   def get_success_url(self):
>     return reverse('home')
>
> class HomeView(TemplateView):
>    template_name = 'user/home.html'
>
>
> components.py:
>
> class Authenticator():
>   def authenticate(self, user, password, request = None):
>     self.user = auth.authenticate(username=user, password=password)
>     if request:
>       return self.login(request)
>
>     return self.user
>
>   def login(self, request):
>     if self.user is not None:
>       if self.user.is_active:
>         auth.login(request, self.user)
>         return self.user
>       else:
>         raise UserDisabledException("Usuario inativo")
>     else:
>       raise InvalidLoginException("Usuario/Senha invalidos")
>
> class UserDisabledException(Exception):
>   def __init__(self, value):
>     self.value = value
>
>   def __str__(self):
>     return repr(self.value)
>
> class InvalidLoginException(Exception):
>   def __init__(self, value):
>     self.value = value
>
>   def __str__(self):
>     return repr(self.value)
>
>
> Thanks again :)
>
> 2012/4/3 Sergiy Khohlov <skhoh...@gmail.com>
>>
>> Please provide your urls.py  and  your view which is used for this ....
>>
>> 2012/4/3 Matheus Ashton <matheusash...@gmail.com>:
>> > Hello Everybody,
>> >
>> > I'm having a problem using the django.contrib.auth app and class based
>> > generic views in Django 1.3 / 1.4:
>> >
>> > After submitting the login form, my view receives the post data and
>> > tries to
>> > authenticate the user and then redirect to a success page. Ok nothing
>> > new..
>> > The problem is: My HomeView wich extends the TemplateView, receive the
>> > redirect from the login form and uses a render_to_response function to
>> > render the template, and that is my problem, because render_to_response
>> > does
>> > not create a RequestContext object, the one I need to show the newly
>> > authenticated user data, because that is a requirement for
>> > django.contrib.auth app, if the request is not a RequestContext, the
>> > authenticated user data, is not passed to the template
>> >
>> > (https://docs.djangoproject.com/en/1.4/topics/auth/#authentication-data-in-templates)..
>> >
>> > Does someone can help me with this problem?
>> >
>> > PS: Sorry about my English, it is not my first language
>> >
>> > Thanks :D
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "Django users" group.
>> > To view this discussion on the web visit
>> > https://groups.google.com/d/msg/django-users/-/ywPX8VH5CUQJ.
>> > 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.
>>
>> --
>> 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.
>>
>
> --
> 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.

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

Reply via email to