Monday, June 3, 2013

CBVs and logging a user


I'm getting an error

Request URL: http://127.0.0.1:8000/heating/login/?next=/heating/orglv/
Django Version: 1.5.1
Exception Type: ImproperlyConfigured
Exception Value:
The included urlconf main.urls doesn't have any patterns in it

my login form and view has been modified to work off email and password rather than username and password


my forms.py looks like

class MyCustomLoginForm(forms.Form):
  """A login form for the customer model"""
  email = forms.EmailField(
    label=('Your email address'),
    required=True)
  password = forms.CharField(
    label=('Your password'),
    widget=forms.PasswordInput,
    required=True)
 
  def clean(self):
    """If the user is valid and active then auth them & return the clean_data"""
    clean_data = self.cleaned_data
    try:
      # authenticate the details
      user = authenticate(
          username=clean_data['email'],
          password=clean_data['password'])
      if user and user.is_active:
          self.user = user
          return clean_data
    except (KeyError, AttributeError):
      pass
    raise forms.ValidationError(('Incorrect Password'))

views.py looks like
class LoginView(FormView):
    """Give a view to login"""
    form_class = MyCustomLoginForm
  template_name = 'login.html'
  success_url = reverse ('orglv')

def get_success_url(self):
      return reverse ('orglv')
 
def form_valid(self, form):
      """ if the form is valid, then log the user in"""
      login(self.request, form.user)
      return HttpResponseRedirect(self.get_success_url())

backends.py looks like


class EmailOrUsernameModelBackend(object):
    def authenticate(self, username=None, password=None):
print(username)
print(password)
        try:
    user = Heating_User.objects.get(email=username)
    print(user.username)
print(user.password)
    if user.check_password(password):
print("password ok")
return user
        except Heating_User.DoesNotExist:
    print("crap")
            return None

    def get_user(self, user_id):
        try:
            return Heating_User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

and my login.html looks like

   {% extends "base.html" %}

    {% block title %}Login{% endblock title %}

    {% block content %}
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit" >login</button>
<input type="hidden" name="next" value="{{next}}" />
    </form>
    {% endblock content %}



and my urls.py looks like

urlpatterns = patterns('',
    url(regex=r'login/$',
view=LoginView.as_view(),
name="login"
    ), 
 


has anyone any ideas?

--
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+unsubscribe@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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment