2016-11-23 50 views
0

我为我的Django项目使用自定义用户模型,我可以通过/admin/完美地登录。但是,当我访问/accounts/login并尝试登录时,它只是将我弹回到登录页面而没有登录。我正在使用django-registration-redux以及简单的后端。django-registration-redux自定义用户模型登录只能通过/ admin/

通过记录我发现,这种方法发生在django.contrib.auth.__init__.py错误:

def get_user(request): 
    """ 
    Returns the user model instance associated with the given request session. 
    If no user is retrieved an instance of `AnonymousUser` is returned. 
    """ 
    from .models import AnonymousUser 
    user = None 
    try: 
     # 
     # EXCEPTION THROWN ON BELOW LINE 
     # 
     user_id = _get_user_session_key(request) 
     backend_path = request.session[BACKEND_SESSION_KEY] 
    except KeyError: 
     pass 
    else: 
     if backend_path in settings.AUTHENTICATION_BACKENDS: 
      backend = load_backend(backend_path) 
      user = backend.get_user(user_id) 
      # Verify the session 
      if hasattr(user, 'get_session_auth_hash'): 
       session_hash = request.session.get(HASH_SESSION_KEY) 
       session_hash_verified = session_hash and constant_time_compare(
        session_hash, 
        user.get_session_auth_hash() 
       ) 
       if not session_hash_verified: 
        request.session.flush() 
        user = None 

    return user or AnonymousUser() 

任何想法? /accounts/register/按预期执行,尽管我已覆盖RegistrationView。也许我必须为登录做同样的事情?

的login.html

{% extends "base.html" %} 
{% load staticfiles %} 
{% block body_block %} 
<link href="{% static 'css/signin.css' %}" rel="stylesheet"> 

<div class="container"> 
    <div class="jumbotron"> 
     <h1 class="display-3" align="center">Login</h1> 
    </div> 
    <form method="post" action="."> 
     {% csrf_token %} 
     <h2 class="form-signin-heading">Please sign in</h2> 
     <label for="inputEmail" class="sr-only">Username</label> 
     <input type="text" name="email" id="id+username" class="form-control" placeholder="Username" required autofocus> 
     <label for="inputPassword" class="sr-only">Password</label> 
     <input type="password" name="password" id="id_password" class="form-control" placeholder="Password" required> 
     <button class="btn btn-lg btn-primary btn-block" type="submit" value="Submit">Login</button> 
    </form> 

    Not a member? 
    <a href="{% url 'registration_register' %}">Register</a> 
</div> 
    <p> 
    </p> 
{% endblock %} 

Urls.py

class MyRegistrationView(RegistrationView): 
    success_url = '/' 
    form_class = UserProfileRegistrationForm 

def get(self, request, *args, **kwargs): 
    form = self.form_class(initial=self.initial) 
    return render(request, self.template_name, {'form': form}) 

def register(self, form): 
    logging.debug("THIS IS MY REGISTER") 
    new_user = form.save(commit=False) 
    new_user.set_password(form.cleaned_data['password1']) 
    new_user.save() 

    login(self.request, new_user) 
    logging.debug("Logged in") 
    signals.user_registered.send(sender=self.__class__, 
           user=new_user, 
           request=self.request) 
    logging.debug("After signals") 
    return new_user 

urlpatterns = [ 
    url(r'^', include('base.urls')), 
    url(r'^admin/', admin.site.urls), 
    url(r'^accounts/register/$', MyRegistrationView.as_view(), name="registration_register"), 
    url(r'^accounts/password/change/$', MyRegistrationView.as_view(), name="auth_password_change"), 
    url(r'^accounts/password/change/done/$', MyRegistrationView.as_view(), name="auth_password_changed"), 
    url(r'^accounts/', include('registration.backends.simple.urls')), 
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

编辑:

我有投掷视图到登录在网址的一个临时的解决.py。有些东西告诉我这是非常肮脏的,但它现在似乎工作。我愿意接受更好的选择。

url(r'^accounts/login/$', my_view, name="login"), 

def my_view(request): 
    if request.POST: 
     username = request.POST['email'] 
     password = request.POST['password'] 
     user = authenticate(username=username, password=password) 
     if user is not None: 
      login(request, user) 
      return render(request, 'index.html', {}) 
      # Redirect to a success page. 
     else: 
      # Return an 'invalid login' error message. 
      pass 
    else: 
     return render(request, 'registration/login.html', {}) 
+1

您需要使用登录重定向URL在您的settings.py file.Read这样的:https://docs.djangoproject.com/en/1.10/ref/settings/#login-redirect-url –

回答

0

在登录模板中尝试使用{{ form }},而不是手动呈现字段。这可以显示问题出在您的模板还是其他地方。

在这种情况下,我认为表格字段应该是usernamepassword,而不是emailpassword

<input type="text" name="username" id="id_username" class="form-control" placeholder="Username" required autofocus> 
<input type="password" name="password" id="id_password" class="form-control" placeholder="Password" required>