Форум сайта python.su
помогите пожалуйста исправить процесс авторизации.
я создаю форму AuthenticationCustomForm (наследуя от AuthenticationForm):
from django.contrib.auth.forms import AuthenticationForm class AuthenticationCustomForm(AuthenticationForm): username = forms.CharField( label='Имя пользователя', widget=forms.TextInput(attrs={ 'placeholder': 'Логин', }), ) password = forms.CharField( label='Пароль', widget=forms.PasswordInput(attrs={ 'placeholder': 'Пароль', }), )
def login(request): if(request.method == "POST"): form = AuthenticationCustomForm(request.POST) with open(os.path.join(settings.BASE_DIR, "1.txt"), "wb") as f: f.write(bytes('1', 'UTF-8')) if form.is_valid(): with open(os.path.join(settings.BASE_DIR, "2.txt"), "wb") as f: f.write(bytes('2', 'UTF-8')) username = request.POST.get('username', '') password = request.POST.get('password', '') user = auth.authenticate(username=username, password=password) if user is not None and user.is_active: auth.login(request, user) return HttpResponseRedirect('/' + str(request.user.pk) + '/') else: form = AuthenticationCustomForm() t = loader.get_template('accounts/login.html') c = RequestContext(request, { 'form': form, }, [custom_proc]) return HttpResponse(t.render(c))
{% for field in form %} {{ field.error }} {% endfor %} <form class="login_form" action="{% url 'login' %}" method="post"> {% csrf_token %} <div class="cell"> {{ form.username }} {{ form.username.errors }} {{form.non_field_errors}} </div> <div class="cell"> {{ form.password }} {{ form.password.errors }} {{form.non_field_errors}} </div> <div class="cell"> <input class="submit btn btn-info" type="submit" value="Войти" /> </div> </form>
url(r'^accounts/login/$', 'views.login', name='login', ),
Офлайн
AuthenticationForm, от которого вы наследуете AuthenticationCustomForm, отличается своеобразным методом __init__, который зачем-то первым после self аргументом принимает request. Хорошо хотя бы, что передавать его туда не обязательно. Попробуйте сделать так:
form = AuthenticationCustomForm(data=request.POST)
Отредактировано pythonlove (Май 8, 2014 15:29:17)
Офлайн
вот спасибо. заработало
Офлайн