gpt4 book ai didi

python - 登录 Django 1.5

转载 作者:太空狗 更新时间:2023-10-30 01:09:15 25 4
gpt4 key购买 nike

我在 Django 1.5 中使用 MyUser 模型和电子邮件登录:型号:

class MyUser(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
db_index=True,
)
last_name=models.CharField(max_length=30)
first_name=models.CharField(max_length=30)
second_name=models.CharField(max_length=30, blank=True)
post=models.CharField(max_length=30, blank=True)

objects = MyUserManager()

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['last_name','first_name','second_name','post', ....]

def get_full_name(self):
return self.email

def get_short_name(self):
return self.email

def __unicode__(self):
return self.email

def has_perm(self, perm, obj=None):
return True

def has_module_perms(self, app_label):
return True

@property
def is_staff(self):
return self.is_admin

我尝试了使用 from django.contrib.auth.views import login 的方法:

网址:

(r'^login/$', login, {'template_name': 'enter.html'}),

View :无。

它适用于经典用户模型中的 super 用户,不适用于 MyUser。

然后我尝试了:

查看:

def login_view(request):
username = request.POST['email']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None and user.is_active:
login(request, user)
return HttpResponseRedirect("/n1.html")# Redirect to a success page.
else:
return HttpResponseRedirect("/account/invalid/")# Return a 'disabled account' error message

模板:

{% if form.errors %}
<p>Something is wrong</p>
{% endif %}

<form action="" method="post">
{% csrf_token %}
<label for="email">Login:</label>
<input type="text" name="email" value="" id="email"/>
<label for="password">Password:</label>
<input type="password" name="password" value="" id="username">

<input type="submit" value="login" />
<input type="hidden" name="next" value="{{next|escape}}" />

</form>

网址:

(r'^login/$', login_view, {'template_name': 'enter.html'}),

但是我得到了错误 login_view() got an unexpected keyword argument 'template_name'我做错了什么?

最佳答案

login_view() got an unexpected keyword argument 'template_name' 意味着你的 View 函数应该有 template_name 参数:

def login_view(request, template_name):
'your code'

如果你不需要它,不要在urls.py中传递它:

(r'^login/$', login_view),

更新。

您的 login_view 处理您的 POST 方法。您可以用这种方式重写它以在 GET 上呈现表单

def login_view(request):
if request.method == 'POST':
username = request.POST['email']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None and user.is_active:
login(request, user)
return HttpResponseRedirect("/n1.html")
return HttpResponseRedirect("/account/invalid/")
form = LoginForm()
return render(request, 'enter.html', {'login_form': LoginForm})

关于python - 登录 Django 1.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15018772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com