0

我正在设计这个应用程序并遇到一个不寻常的错误,试图修复但我看不到错误出现的位置。我一直在尝试比较代码以查看是否可以找到错误,但两个代码只是相同我的代码不起作用

该教程来自这里http://hackedexistence.com/project/django/video7-userauthentication-2.html

 SyntaxError at /register/

 ('invalid syntax', ('C:\\djcode\\mysite\\drinker\\views.py', 23, 12, '        else:\n'))

 Request Method:    GET
 Request URL:   http://127.0.0.1:8000/register/
 Django Version:    1.4.3
 Exception Type:    SyntaxError
 Exception Value:   

 ('invalid syntax', ('C:\\djcode\\mysite\\drinker\\views.py', 23, 12, '        else:\n'))

 Exception Location:    C:\Python26\Lib\site-packages\django\utils\importlib.py in      import_module, line 35
 Python Executable:     C:\Python26\python.exe
 Python Version:    2.6.0

我的观点.py

 from django.http import HttpResponseRedirect
 from django.contrib.auth.models import User
 from django.contrib.decorators import login_required
 from django.shortcuts import render_to_response
 from django.template import RequestContext
 from drinker.forms import RegistrationForm,LoginForm
 from drinker.models import Drinker
 from django.contrib.auth import authenicate ,login , logout

 def DrinkerRegistration(request):
     if request.user.is_authenticated():
         return HttpResponseRedirect('/profile/')
     if request.method == 'POST':
         form = RegistrationForm(request.POST)
         if form.is_valid():
             user = User.objects.create_user(username=form.cleaned_data['username'], email = form.cleaned_data['email'] , password = form.cleaned_data['password'])
             user.save()
             drinker = Drinker(user=user , name=form.cleaned_data['name'] , birthday =form.cleaned_data['birthday'])
             drinker.save()
             return HttpResponseRedirect('/profile/')
         else:
             return render_to_response('register.html',{'form':form}, context_instance=RequestContext(request))
         else:
             form = RegistrationForm()
             context = {'form':form}
             return render_to_response('register.html',context,context_instance=RequestContext(request))
 def LoginRequest(request):
     if request.user.is_authenticated():
         return HttpResponseRedirect('/profile/')
     if request.method == 'POST':
         form= LoginForm(request.POST)
         if form.is_valid():
             username = form.cleaned_data['username']
             password = form.cleaned_data['password']
             drinker = authenticate(username=username ,password=password)
             if drinker is not None:
                 login(request,drinker)
                 return HttpResponseRedirect('/profile/')
             else:
                 return render_to_response('login.html',{'form':form} , context_instance=RequestContext(request))
             else:
                 return render_to_response('login.html',{'form': form}, context_instance=RequestContext(request))
             else:
                 form= LoginForm()
                 context = {'form':form}
            return render_to_response('login.html',context,context_instance=RequestContext(request))
 def LogoutRequest(request):
     logout(request)
     return HttpResponseRedirect('/')
4

1 回答 1

1

你有两个else:条件。这不是有效的语法。转到您提供的链接,您会出现缩进错误。应该是这样的。一个用于内部if-else,另一个用于外部if-else

    if request.method == 'POST':
            form = RegistrationForm(request.POST)
            if form.is_valid():
                    user = User.objects.create_user(username=form.cleaned_data['username'], email = form.cleaned_data['email'], password = form.cleaned_data['password'])
                    user.save()
                    drinker = Drinker(user=user, name=form.cleaned_data['name'], birthday=form.cleaned_data['birthday'])
                    drinker.save()
                    return HttpResponseRedirect('/profile/')
            else:
                    return render_to_response('register.html', {'form': form}, context_instance=RequestContext(request))
    else:
            ''' user is not submitting the form, show them a blank registration form '''
            form = RegistrationForm()
            context = {'form': form}
            return render_to_response('register.html', context, context_instance=RequestContext(request))
于 2013-02-20T14:53:13.270 回答