gpt4 book ai didi

python - 为什么不以验证的形式工作?

转载 作者:太空宇宙 更新时间:2023-11-04 03:46:30 25 4
gpt4 key购买 nike

请帮忙改正形状。

我创建了一个表单来输入 AuthenticationCustomForm(从 AuthenticationForm 继承):

from django.contrib.auth.forms import AuthenticationForm

class AuthenticationCustomForm(AuthenticationForm):
username = forms.CharField(
label='Имя пользователя',
widget=forms.TextInput(attrs={
'placeholder': 'Логин',
}),
)

password = forms.CharField(
label='Пароль',
widget=forms.PasswordInput(attrs={
'placeholder': 'Пароль',
}),
)

然后在views.py中创建:

def login(request):
if(request.method == "POST"):
form = AuthenticationCustomForm(request.POST)
with open(os.path.join(settings.BASE_DIR, "1.txt"), "wb") as f:
f.write(bytes('1', 'UTF-8'))
if form.is_valid():
with open(os.path.join(settings.BASE_DIR, "2.txt"), "wb") as f:
f.write(bytes('2', 'UTF-8'))
username = request.POST.get('username', '')
password = request.POST.get('password', '')

user = auth.authenticate(username=username, password=password)

if user is not None and user.is_active:
auth.login(request, user)
return HttpResponseRedirect('/' + str(request.user.pk) + '/')
else:
form = AuthenticationCustomForm()

t = loader.get_template('accounts/login.html')
c = RequestContext(request, {
'form': form,
}, [custom_proc])
return HttpResponse(t.render(c))

登录.html:

<form class="login_form" action="{% url 'login' %}" method="post">
{% csrf_token %}

<div class="cell">
{{ form.username }}

{{ form.username.errors }}
</div>

<div class="cell">
{{ form.password }}

{{ form.password.errors }}
</div>

<div class="cell">
<input class="submit btn btn-info" type="submit" value="Войти" />
</div>
</form>
</form>

urls.py:

urlpatterns = patterns('drummersaransk',
url(r'^accounts/auth/$', 'views.auth_view', name='auth_view', ),
url(r'^accounts/logout/$', 'views.logout', name='logout', ),
url(r'^accounts/login/$', 'views.login', name='login', ),
url(r'^accounts/loggedin/$', 'views.loggedin', ),
url(r'^accounts/invalid_login/$', 'views.invalid_login', ),
)

问题是表格不起作用。该文件是'1。 txt '写入和文件'2。 txt' 没有记录。

最佳答案

你可以这样做:

urls.py:

from django.contrib.auth.views import login
from hello.forms import AuthenticationCustomForm
url(r'^accounts/login/$', login , {'authentication_form':AuthenticationCustomForm,'template_name':'login.html'} ,name='login'),

关于python - 为什么不以验证的形式工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23534615/

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