认证工作不进行自定义验证 [英] Authentication is not working for custom authentication
问题描述
我想进行自定义登录页面在Django。我可以成功注册的用户,但是当我要登录它没有获得登录。
I want to make custom login page in Django. I can successfully register user but when I want to login it did not get log in.
我的 views.py
进行登录:
def login_user(request):
context = RequestContext(request)
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
print username
print password
user = authenticate(username=username, password=password)
if user:
print "rrrrr"
if user.is_active:
login(request, user)
return HttpResponseRedirect('/success/')
else:
return HttpResponse("Your Rango account is disabled.")
else:
print "Invalid login details: {0}, {1}".format(username, password)
return HttpResponse("Invalid login details supplied.")
else:
return render_to_response('login.html', {}, context)
有关注册我写这篇code:
For registration I had write this code:
def register(request):
context = RequestContext(request)
registered = False
print "inside register view"
if request.method == 'POST':
profile_form = ProfileForm(data=request.POST)
if profile_form.is_valid():
profile = profile_form.save(commit=False)
profile.save()
registered = True
else:
print profile_form.errors
else:
profile_form = ProfileForm()
return render_to_response(
'register.html',
{'profile_form': profile_form, 'registered': registered},
context)
我的 models.py
:
class Profile(models.Model):
# user=models.OneToOneField(User)
username=models.CharField(max_length=50)
first_name=models.CharField(max_length=50)
last_name=models.CharField(max_length=50)
email=models.EmailField()
contact=models.CharField(max_length=50)
password=models.CharField(max_length=50)
def __str__(self):
return self.username or u'
在我公司供应用户名
和密码
它接受电子邮件,并通过它,但始终进入的其他部分的无效的登录信息
。它是不会进入的如果条件的。我怎么能做出这样的工作?
When I supply username
and password
it accepts the email and pass it but always go into the else portion: Invalid login details
. It is not going into the if condition. How can I make working this?
推荐答案
看来你不存储在 django.contrib.auth.models.User
的任何数据的应该是这样的:
It seems you don't store any data in django.contrib.auth.models.User
your should be something like this:
models.py
class Profile(models.Model):
user = models.OneToOneField(User)
contact = models.CharField(maX_length = 50)
# User class contain other fields that you need.
forms.py
class UserForm(forms.Form):
class Meta:
model = User
class ProfileForm(forms.Form):
class Meta:
model = Profile
registeration code:
def register(request):
# ...
# Your codes here
profile_form = ProfileForm(data=request.POST)
# Add this line:
user_form = UserForm(data=request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
profile = profile_form.save(commit=False)
profile.save()
registered = True
else:
print profile_form.errors
print user_forms.errors
else:
profile_form = ProfileForm()
user_form = UserForm()
return render_to_response(
'register.html',
{'profile_form': profile_form, 'user_form':user_form 'registered': registered},
context)
您可以找到更多的Django的正式用户领域这里
You can find more Django official User fields here
这篇关于认证工作不进行自定义验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!