E
E
Entropie2016-10-31 16:10:19
Django
Entropie, 2016-10-31 16:10:19

Why doesn't login() work in Django?

Authorization works only for the admin (both regular and through the admin panel). For any other user for whom the registration was definitely passed, there are no password and login matches, although they are exactly correct Authorization is standard (login function)
urls.py

from .views import *
from django.contrib import admin
from django.contrib.auth.views import logout, login
from django.contrib.auth.decorators import login_required

urlpatterns = [
    url(r'^$', home, name = 'main'),
    url(r'^users/$', UserList.as_view(), name = 'users'),
    url(r'^users/user/(?P<slug>\w+)/', AccountView.as_view(), name = 'user'),
    url(r'^blogs/blog/(?P<slug>\w+)/', BlogView.as_view(), name = 'blog'),
    url(r'^login/', login, {'template_name':'login.html'}, name = 'login'),
    url(r'^logout/', login_required(logout), {'template_name': 'logout.html'}, name = 'logout'),
    url(r'^registration/', register, name = 'registration'),
]

forms.py
# *-* coding:utf-8 *-*
from django import forms
from django.contrib.auth.forms import UserCreationForm
from .models import User

class RegisterForm(UserCreationForm):

    class Meta:
        model = User
        fields = ('username',
                    'email',)


    def save(self, commit = True):
        user = super(RegisterForm, self).save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        user.email = self.cleaned_data["email"]
        if commit:
            user.save()
        return user

views.py
# *-* coding: utf-8 *-*
from django.contrib.auth import authenticate
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.views import login
from django.shortcuts import render, render_to_response
from django.shortcuts import HttpResponse, HttpResponseRedirect, reverse, redirect
from django.template import RequestContext
from django.views.generic import DetailView, ListView
from django.views.generic.edit import FormView,CreateView
from .models import User, UserManager
from .forms import RegisterForm

# Create your views here.
class AccountView(DetailView):
    model = User
    template_name = "users/account.html"
    context_object_name = 'user'
    slug_field = 'username'


class UserList(ListView):
    template_name = "users/user_list.html"
    context_object_name = 'users'
    model = User

class BlogView(DetailView):
    template_name = "users/blog.html"
    context_object_name = 'blog'
    model = User
    slug_field = 'username'

def register(request):
    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            user = User(username=form.cleaned_data['username'],
            password=form.cleaned_data['password1'],
            email=form.cleaned_data['email'])
            user.save()
            us = authenticate(username = user.username, password = user.password)  
            login(request, us)
            return HttpResponseRedirect('/')
    form = RegisterForm()
    return render(request, 'registration.html',{'form':form}) 

def home(request):
    return render(request, 'main.html')

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question