python - 为什么验证形式不起作用?

标签 python django

请帮忙纠正形状。

我创建一个表单来输入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>

url.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/

相关文章:

python - 如何告诉 HTTP 服务器不发送分块编码

python - Hadoop流-昂贵的共享资源(COOL)

python - 在安装 Python 3.4 的同时为 Python 2.7 安装 numpy?

python - 使导航栏项目在 Django 模板中处于事件状态

python - Django,在模板中创建变量并赋值

django - Feed算法+数据库: Either too many rows or too slow retrieval

python - 模块未找到错误: No module named 'tensorflow.contrib.framework'

Django/Mod_WSGI 'client denied by server configuration'

django - 传递用户,请求表单

python - SQLITE 3安装错误