2016-12-15 11 views
0

私はユーザをログインしようとしていますが、私のビューは動作していないようです。ページがレンダリングされ、フォームが表示されます。しかし、有効なユーザー名とパスワードを入力すると、success_urlに行く代わりにページが更新されます。私はそれが私のビューメソッドを実装している方法だと思う。私はlogin(request, user_obj)がアクセスされているとは思わない、もしそうならばrequest.user.is_authenticated()です。Djangoでクラスベースのビューを使用しているユーザをログに記録する

私も

{% if user.is_authenticated %} 
    {% include 'navbar_in.html' %} 
{% else %} 
    {% include 'navbar_out.html' %} 
{% endif %} 

を使用して、私のログインフォームを含めるそれだけnavbar_out.htmlを使用していますので、それは私が私のコードはlogin(request, user_obj)にアクセスしていない知っている方法です。

Userモデル:

# Create your models here. 
class Usermie(models.Model): 
    # associate fields with django built in user objects 
    # this provides the authentication that we would need from the django built in utilities 
    usermie_object = models.OneToOneField(User) 

    # form fields 
    id = models.AutoField(primary_key=True) 
    username = models.CharField(max_length=30, unique=True, blank=False) 
    email = models.EmailField(max_length=30, unique=True, blank=False, null=False) 
    first_name = models.CharField(max_length=30, blank=False) 
    last_name = models.CharField(max_length=30, blank=False) 
    birthday = models.DateField(blank=False) 
    password = models.CharField(max_length=50, null=False) 
    sex = models.CharField(max_length=1, blank=False) 
    location = models.CharField(max_length=100, null=True) 
    city = models.CharField(max_length=40, null=True) 
    province = models.CharField(max_length=40, null=True) 
    country = models.CharField(max_length=40, null=True) 
    activated = models.IntegerField() # will be 1 if account is activated, 0 otherwise. 
    date_created = models.DateTimeField(default=timezone.now) # the time the account is created 
    date_activated = models.DateTimeField(null=True) # when the account is activated via email 

    def __str__(self): 
     return self.username 


# create a user object to attach to our forms (SignUp)object 
def create_usermie_user_callback(sender, instance, **kwargs): 
    usermie, new = Usermie.objects.get_or_create(usermie_object=instance) 

ビュー:

from .models import Usermie 
from django.shortcuts import render 
from django.views.generic import View 
from .forms import SignUpForm, LoginForm 
from django.contrib.auth.models import User 
from django.http import HttpResponseRedirect 
from django.views.generic.base import TemplateView 
from django.contrib.auth import authenticate, login, logout 

class UserLoginRegistration(View): 
    form_class = LoginForm 

    # Use initial to declare the initial value of form fields at runtime. 
    # For example, you might want to fill in a username field with 
    # the username of the current session. 
    initial = {'key': 'value'} 
    template_name = 'usermie/usertest.html' # template form will be rendered on 
    success_url = '/usermie/home/' # template for successfully submitted form 

    def get(self, request, *args, **kwargs): 

     form_login = self.form_class(initial=self.initial) 

     return render(request, self.template_name, {'form_login': form_login}) 

    # method for posting form 
    def post(self, request, *args, **kwargs): 

     # Accessing form with post data 
     form_login = self.form_class(request.POST) 

     # Checking if user is logged in 
     if request.user.is_authenticated(): 
      # Making sure user does not log in twice, 
      # just send user to profile.html if already logged in 
      return HttpResponseRedirect(self.success_url) 

     # Checking if the form is valid 
     if form_login.is_valid(): 
      email = form_login.cleaned_data['email'] 
      password = form_login.cleaned_data['password'] 

      # NB! On Django docs Two methods to authenticate user and log them in 
      # 1 call authenticate 
      # 2 call login 

      # Return a user_obj object if the username and password are valid 
      # otherwise it will return null, the null variable is called None in python 
      user_obj = authenticate(email=email, password=password) 
      if user_obj is not None: 
       if user_obj.is_active: 
        login(request, user_obj) 
        return HttpResponseRedirect(self.success_url) 

      # If authentication failed 
      else: 
       return HttpResponseRedirect(self.template_name) 
     # If form is not being posted, render form to template 
     else: 
      form_login = self.form_class(initial=self.initial) 
      context = {'form_login': form_login} 
      return render(request, self.template_name, context) 

そして、これはこれは、ユーザーがアクティブでないと、そうでないことかもしれません

<form class="navbar-form navbar-form-out" action="" method="post"> 
    {% csrf_token %} 
    {% load widget_tweaks %} 
    <div class="form-group"> 
     <label class="sr-only" for="{{ form_login.email.auto_id }}">{{ form_login.email.label }}</label> 
     {% render_field form_login.email class="form-control" placeholder=form_login.email.label %} 
    </div> 
    <div class="form-group"> 
     <label class="sr-only" for="{{ form_login.auto_id }}">{{ form_login.password.label }}</label> 
     {% render_field form_login.password class="form-control" placeholder=form_login.password.label %} 
     {% for hidden in form_login.hidden_fields %} 
      {{ hidden }} 
     {% endfor %} 
    </div> 
    <div class="checkbox"> 
     <label> 
      <input type="checkbox"> Remember me 
     </label> 
    </div> 
    <button type="submit" name="" value="form_login" class="btn btn-default">Sign in</button> 
</form> 
+1

デフォルトのユーザモデルでは、 'authenticate'は' email'と 'password'ではなく' username'と 'password'を想定しています。あなたはあなたのユーザモデルを表示していないので、 'email'と' password'があなたのために働くかどうかはわかりません。 – Alasdair

+0

これをモデルでチェックしてみましょう。私はあなたに戻ってきます。 – Jam1

+0

自分のフォームでユーザー名に電子メールを変更しましたが、作成したユーザーには何も問題はありませんでしたが、管理者のユーザー名とパスワードでログインしても正しく動作します。私は何が起こっているのか分からない。 – Jam1

答えて

0

アップ私のマークですそのケースを扱う。

if user_obj.is_active: 
    login(request, user_obj) 
    return HttpResponseRedirect(self.success_url) 
else: 
    return HttpResponse("Your account is inactive.") 
+0

それはまだメッセージなしで同じページを更新します。 – Jam1

関連する問題