Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么不以验证的形式工作?_Python_Django - Fatal编程技术网

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

Python 为什么不以验证的形式工作?,python,django,Python,Django,请帮我纠正一下形状 我创建一个表单以输入AuthenticationCustomForm(从AuthenticationForm继承): 然后在views.py中创建: def login(request): if(request.method == "POST"): form = AuthenticationCustomForm(request.POST) with open(os.path.join(settings.BASE_DIR, "1.txt"

请帮我纠正一下形状

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

然后在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)) 
login.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>

问题是表单不起作用。文件是'1'。txt“已写入和文件”2。未记录txt。

在AuthenticationForm clean()方法中检查用户是否存在并处于活动状态:

因此,您似乎传递了错误的登录名或密码。只需输出{form.errors}},您就会看到错误。

您可以改为:

url.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'),
解决办法是:

form = AuthenticationCustomForm(data=request.POST)

form.u是否有效?是否无效?您将在{{form.password.error}和{{form.username.error}错误中看到什么错误,这些错误不会显示出来,因此请向我们显示URL的内容。py@MortezaIpo你为什么这么肯定?对不起。这似乎是我的错误,这个错误应该通过{{form.errors}}显示。现在,成功的身份验证后,重定向失败。在用户登录到这里之后,返回HttpResponseRedirect('/'+str(request.user.pk)+'/'),然后他进入一个带有授权表单的页面是的,我已经检查了,但没有找到任何方法。这里的解决方案是:form=AuthenticationCustomForm(data=request.POST)这很好。但当您重定向到/accounts/profile/时(以我的方式),它将显示您的详细信息,并且不需要url中的用户ID。
def clean(self):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')

        if username and password:
            self.user_cache = authenticate(username=username,
                                           password=password)
            if self.user_cache is None:
                raise forms.ValidationError(
                    self.error_messages['invalid_login'],
                    code='invalid_login',
                    params={'username': self.username_field.verbose_name},
                )
            elif not self.user_cache.is_active:
                raise forms.ValidationError(
                    self.error_messages['inactive'],
                    code='inactive',
                )
        return self.cleaned_data
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'),
form = AuthenticationCustomForm(data=request.POST)