Django SyntaxError:语法无效' else'声明?

时间:2018-05-15 00:44:26

标签: django syntax

为了上帝的缘故,我无法理解为什么我会收到这个错误,它看起来很简单,但我现在做了很多改动而且不知道到底是什么造成这种情况:

(from Traceback):

File "C:\Users\Lucas Cyrne Ferreira\Desktop\django- tutorial\mysite\contas\urls.py", line 2, in <module>
from . import views
File "C:\Users\Lucas Cyrne Ferreira\Desktop\django-tutorial\mysite\contas\views.py", line 38
else:
   ^

SyntaxError:语法无效

from django.shortcuts import render, redirect, HttpResponse
from contas.forms import (
RegistrationForm,
EditPerfilForm,
)
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserChangeForm, PasswordChangeForm
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.decorators import login_required


def home(request):
    return render(request, 'contas/home.html')

def register(request):
    if request.method=='POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect(reverse('home:home'))
    else:
        form = RegistrationForm()

    args = {'form':form}
    return render(request, 'contas/reg_form.html', args)

def view_perfil(request):
    args = {'user': request.user}
    return render(request, 'contas/perfil.html', args)

def edit_perfil(request):
    if request.method=='POST':
        form = EditPerfilForm(request.POST, instance=request.user)

        if form.is_valid():
            form.save()
            return redirect(reverse('home:perfil')
    else:
        form = EditPerfilForm(instance=request.user)
        args = {'form':form}
        return render(request, 'contas/edit_perfil.html', args)

def trocar_password(request):
    if request.method=='POST':
        form = PasswordChangeForm(data=request.POST, user=request.user)

        if form.is_valid():
            form.save()
            update_session_auth_hash(request, form.user)
            return redirect(reverse('home:perfil')
        else:
            return redirect(reverse('home:trocar_password'))
    else:
        form = PasswordChangeForm(user=request.user)

        args = {'form': form}
        return render(request, 'contas/trocar_password.html', args)

我的contas \ urls.py:

from django.urls import path
from . import views
from django.contrib.auth.views import (
    login,
    logout,
    PasswordResetView,
    PasswordResetDoneView,
    PasswordResetConfirmView,
    PasswordResetCompleteView,
    )

app_name = 'contas'
urlpatterns = [

    path('', views.home, name='home'),
    path('login/', login, {'template_name': 'contas/login.html'}, name='login'),
    path('logout/', logout, {'template_name': 'contas/logout.html'}, name='logout'),
    path('register/', views.register, name='register'),
    path('perfil/', views.view_perfil, name='view_perfil'),
    path('perfil/edit/', views.edit_perfil, name='edit_perfil'),
    path('trocar-password/', views.trocar_password, name='trocar_password'),
    path('reset-password/', PasswordResetView.as_view(), {'template_name': 'contas/reset_password.html', 'post_reset_redirect': 'contas:password_reset_done'},
name='reset_password'),
    path('reset-password/done/', PasswordResetDoneView.as_view(), name='password_reset_done'),
    path('reset-password/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/',
PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
    path('reset-password/complete/', PasswordResetCompleteView.as_view(), name='password_reset_complete'),

]

请帮助我们,我正处于命名空间教程的中间......现在我已经陷入了困境。

1 个答案:

答案 0 :(得分:1)

第39行你的右括号缺失了:

return redirect(reverse('home:perfil')  # missing )

我绝对建议您在项目中使用flake8这样的linter,并为您使用的任何代码编辑器或IDE查找插件。从长远来看,它会发现错误,并为您节省很多悲伤。