登录后用户未经过身份验证

这是我第一次使用Django,我正在尝试创建一个简单的登录页面 . 我希望登录页面位于站点的基本URL(索引),然后使用“assignments /”重定向到“assignments /”,并且只有用户登录才能访问站点上的所有其他页面 . 我尝试查看用户是否经过身份验证(使用“request.user.is_authenticated”),该视图处理用户就像他/她没有登录一样 .

views.py

from django.contrib.auth import login as dj_login

def login(request):
    return render(request, 'mainapp/login.html')


def assignments(request):
    if request.user.is_authenticated:
        assignment_list = Assignment.objects.all()
        context_dict = {'assignments': assignment_list}
        return render(request, 'mainapp/assignments.html', context_dict)
    else:
        return HttpResponse("You must login first")

def show_assignment(request, assignment_name_slug):
    try:
        context_dict = {}
        assignment = Assignment.objects.get(slug=assignment_name_slug)
        context_dict['assignment'] = assignment
    except Category.DoesNotExist:
        context_dict['assignment'] = None
    return render(request, 'mainapp/projectpage.html', context_dict)



def user_login(request):
    information.
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(username=username, password=password)
        if user:
            if user.is_active:
                dj_login(request, user)
                return HttpResponseRedirect(reverse('assignments'))
            else:
                return HttpResponse("Your account is disabled.")
        else: 
            print("Invalid login details: {0}, {1}".format(username, password))
            return HttpResponse("Invalid login details supplied.")   
    else:
        return HttpResponse("You must login first")

应用程序urls.py

urlpatterns = [
    url(r'^$', views.assignments, name='assignments'),
    url(r'^(?P<assignment_name_slug>[\w\-]+)/$',
        views.show_assignment, name='show_assignment')
]

项目urls.py

urlpatterns = [
        url(r'^$', views.login, name="login"),
        url(r'^assignments/', include('mainapp.urls')),
        url(r'^admin/', admin.site.urls),
    ]

的login.html

<form id="login_form" method="post" action="{% url 'assignments' %}" class="col-lg-6 mx-auto">
                    <div class="form-group">
                    {% csrf_token %}
                    <input type="text" name="username" value="" class="form-control" placeholder="Username">
                    </div>
                    <div class="form-group">
                      <input type="password" name="password" value="" class="form-control" placeholder="Password">
                    </div>
                    <input type="submit" value="Log In" class="bg-info border-info btn btn-primary"/>
              </form>

中间件

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

回答(1)

2 years ago

你在'任务'方法中忘了()签名

尝试:

request.user.is_authenticated():