Tuesday, April 3, 2012

Re: Django Contrib Auth + Class Based Generic Views

urls.py:

urlpatterns = patterns('',
    url(r'^/login$', LoginView.as_view()),
    url(r'^$', HomeView.as_view(), {}, 'home'),
)

settings.py:

  LOGIN_URL='/login/'

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 <skhohlov@gmail.com>
Please provide your urls.py  and  your view which is used for this ....

2012/4/3 Matheus Ashton <matheusashton@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+unsubscribe@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+unsubscribe@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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate