我想将自己的表单集成到django Auth系统中。它总是抛出错误:
登录此字段为必填项。密码此字段为必填。
在if request.method == 'POST':
上设置调试点无效(在页面开始/加载时除外)。
settings.py:
AUTH_USER_MODEL = 'Immo.ImmoUser'
ImmoUser类:
from django.contrib.auth.models import AbstractUser
class ImmoUser(AbstractUser):
pass
表格:
<form class="form-signin" action="index" method="post" style="/*! height: 100%; */margin-top: 20%;">
<img class="mb-4" src="{% static 'Immo/images/houses.png' %}" style="background-color: black; padding: 5px;border-radius: 10px;" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
{% csrf_token %}
{{ form.errors }}
<label class="sr-only" for="{{ form.username.auto_id }}">{{ form.username.label }}:</label>
<input id="{{ field.username }}" class="form-control" style="margin-bottom:3px;" autofocus="" placeholder="Login*" required="" type="text">
<label class="sr-only" for="{{ form.password.auto_id }}">{{ form.password.label }}:</label>
<input id="{{ field.password }}" class="form-control" style="margin-bottom:3px;" autofocus="" placeholder="Password*" required="" type="password">
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
</form>
views.py:
def mylogin(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username, password=raw_password)
return redirect('Immo/user/index')
else:
form = UserCreationForm()
return render(request, 'Immo/index.html', {'form': form} )
编辑: 这有效:
<form class="form-signin" action="index" method="post" style="/*! height: 100%; */margin-top: 20%;">
<img class="mb-4" src="{% static 'Immo/images/houses.png' %}" style="background-color: black; padding: 5px;border-radius: 10px;" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
{% csrf_token %}
{{ form.errors }}
<label class="sr-only" for="{{ form.login.auto_id }}">{{ form.login.label }}:</label>
<input name="login" id="{{ form.login.auto_id }}" class="form-control" style="margin-bottom:3px;" autofocus="" placeholder="Login*" required="" type="text">
<label class="sr-only" for="{{ form.password.auto_id }}">{{ form.password.label }}:</label>
<input name="password" id="{{ form.password.auto_id }}" class="form-control" style="margin-bottom:3px;" autofocus="" placeholder="Password*" required="" type="password">
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
</form>
答案 0 :(得分:0)
您在inputs
中缺少属性 name :
<input name="username" ...>
<input name="password" ...>