2016-02-05 61 views
0
def register(request): 
    """Register a new user.""" 
    if request.method != 'POST': 
    # Display blank registration form. 
    form = UserCreationForm() 
    else: 
    # Process completed form. 
    form = UserCreationForm(data=request.POST) 
    if form.is_valid(): 
     new_user = form.save() 
     # Log the user in and then redirect to home page. 
     authenticated_user = authenticate(username=new_user.username,password=request.POST['password1']) 
     login(request, authenticated_user) 
     return HttpResponseRedirect(reverse('learning_logs:index')) 
    context = {'form': form} 
    return render(request, 'users/register.html', context) 

我得到了一个错误: TabError:标签的使用也不一致,空间缩进 我得到了^略低于[“密码1”])Python的缩进如何解决

+1

听起来像你正在使用制表符和空格。选一个。 – Andy

+1

你(当然)可以随意挑选你想要的东西。但是,python社区中的大部分遵循PEP8中的建议__4 spaces__的准则。 – mgilson

+1

我宁愿设置一个编辑器来执行PEP8所要求的所有Python代码 – dlmeetei

回答

0

听起来像是你有一个代码中的选项卡和空格混合。 PEP8建议您使用空格

Spaces are the preferred indentation method.

有一个安装包的PyPI,reindent,您可能需要使用的缩进。

0

现有的答案和意见是正确的,但这里的一些细节:

在文本编辑器,把你的光标在authenticated_user = ...开始,然后单击左箭头按钮向左移动光标。你会发现两个空格,然后光标会跳过你的问题中代码中仍然存在的选项卡。该选项卡需要用空格替换,或者需要将所有其他代码转换为使用制表符(但前面提到的空格是首选)。

+0

感谢所有回复。是的,我试着将光标放在开头,然后向左移动,发现前面有一个标签。现在使用空格键,而且都很好。 –