Python Django用户可以';t登录
我做了一个自定义的UserCreationForm。我可以注册,但注册之后我就不能登录我的web应用了。我只能以超级用户身份登录。我导入了LoginView(),但当我尝试登录时,它告诉我“请输入正确的用户名和密码。请注意,这两个字段可能区分大小写。” [views.py]Python Django用户可以';t登录,python,django,Python,Django,我做了一个自定义的UserCreationForm。我可以注册,但注册之后我就不能登录我的web应用了。我只能以超级用户身份登录。我导入了LoginView(),但当我尝试登录时,它告诉我“请输入正确的用户名和密码。请注意,这两个字段可能区分大小写。” [views.py] @csrf_exempt def signup(request): if request.method == 'POST': user_form = UserForm(data=request.POST)
@csrf_exempt
def signup(request):
if request.method == 'POST':
user_form = UserForm(data=request.POST)
profile_form = UserProfileInfoForm(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.user = user
if 'profile_pic' in request.FILES:
print('found it')
profile.profile_pic = request.FILES['profile_pic']
profile.save()
# registered = True
login(request, user, backend='django.contrib.auth.backends.ModelBackend')
return redirect('../')
else:
print(user_form.errors,profile_form.errors)
else:
user_form = UserForm()
profile_form = UserProfileInfoForm()
return render(request,'users/signup.html',
{'user_form':user_form,
'profile_form':profile_form,'registered':registered})
forms.py
class UserForm(UserCreationForm):
class Meta():
model = User
fields = ('username','email')
def __init__(self, *args, **kwargs):
super(UserCreationForm, self).__init__(*args, **kwargs)
for fieldname in ['username', 'password1', 'password2']:
self.fields[fieldname].help_text = None
class UserProfileInfoForm(forms.ModelForm):
class Meta():
is_photographer = forms.BooleanField()
model = UserProfileInfo
fields = ('portfolio_site','profile_pic', 'type_of_photography', 'is_photographer',)```
models.py
class UserProfileInfo(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
portfolio_site = models.URLField(blank=True)
profile_pic = models.ImageField(upload_to='profile_pics',blank=True)
is_photographer = models.BooleanField(default=False)
type_of_photography = models.CharField(max_length=120, blank=True)
def __str__(self):
return self.user.username
login.html
{% load staticfiles %}
{% block title %}
<title>Login</title>
<link rel="stylesheet" href="{% static 'css/login.css' %}"/>
{% endblock %}
{% block main %}
<div class="col-md-11 container login">
<div class="row">
<div class="col-md-7 left">
<div class="group">
<h1>Login</h1>
<h4>Welcome Back. Please login to your account.</h4>
<form enctype="multipart/form-data" method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" value = "login">Login</button>
</form>
</div>
</div>
<div class="col-md-5 right">
<div class="group">
<h1>Don't have an account?</h1>
<h4>Create one now!</h4>
<a href="{% url 'signup' %}"><button>Sign up</button></a>
</div>
</div>
</div>
</div>
{% endblock %}
{%load staticfiles%}
{%block title%}
登录
{%endblock%}
{%block main%}
登录
欢迎回来。请登录您的帐户。
{%csrf_令牌%}
{{form.as_p}}
登录
你没有账户吗?
现在就创建一个!
{%endblock%}
您将如何登录?给我们看代码,我没有任何代码。我在我的url.py中导入了登录视图,然后将其呈现到模板中。我在帖子中添加了login.html。删除这一行:user.set\u password(user.password)
。这会导致双重哈希。您将如何登录?给我们看代码,我没有任何代码。我在我的url.py中导入了登录视图,然后将其呈现到模板中。我在帖子中添加了login.html。删除这一行:user.set\u password(user.password)
。这会导致双重散列。