django自定义用户视图?
Posted
技术标签:
【中文标题】django自定义用户视图?【英文标题】:django customizing user view? 【发布时间】:2017-03-15 00:01:29 【问题描述】:我进行自定义用户。 所以我需要用电子邮件登录。 user = authenticate(email=email, password=password) 似乎不起作用... 帮我... 让我知道如何修复我的views.py。
这是我的看法。
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login, logout
from django.core.urlresolvers import reverse
from django.shortcuts import render
from LanguageExchange.forms import UserCreationForm,UserChangeForm
def index(request):
context_dict = 'boldmessage': "Crunchy, creamy, cookie, candy, cupcake!"
return render(request, 'LanguageExchange/index.html', context=context_dict)
def register(request):
# A boolean value for telling the template
# whether the registration was successful.
# Set to False initially. Code changes value to
# True when registration succeeds.
registered = False
if request.method == 'POST':
user_form = UserCreationForm(data=request.POST)
# change_form = UserChangeForm(data=request.POST)
# if the two forms are valid..
if user_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
#if change_form.is_valid():
# change = change_form.save()
# change.set_password(user.password)
# change.user = user
registered = True
else:
print(user_form.errors)
else:
user_form = UserCreationForm()
# change_form = UserChangeForm()
return render(request,
'LanguageExchange/register.html',
'user_form': UserCreationForm,
'registered': registered)
def user_login(request):
if request.method == 'POST':
username = request.POST.get('email')
password = request.POST.get('password')
user = authenticate(email=email, password=password)
if user:
if user.is_active:
login(request, user)
return HttpResponseRedirect(reverse('index'))
else:
return HttpResponse("Your Rango account is disabled.")
else:
print("Invalid login details: 0, 1".format(email, password))
return HttpResponse("Invalid login details supplied.")
else:
return render(request, 'LanguageExchange/login.html', )
让我知道如何修复我的views.py。
【问题讨论】:
【参考方案1】:删除
user.set_password(user.password)
在注册中;
改变
user = authenticate(email=email, password=password)
到
user = authenticate(username=email, password=password)
在用户登录中。
【讨论】:
让我知道我的问题是什么? ? 看一下UserCreationForm的源代码就知道了。现在可以登录了吗?以上是关于django自定义用户视图?的主要内容,如果未能解决你的问题,请参考以下文章