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

私のviews.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

あなたには2つelse:の条件があります。これは有効な構文ではありません。あなたが提供したリンクに行くと、インデントエラーが発生しています。このようにする必要があります。もう 1 つはインナー用if-else、もう 1 つはアウター用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 に答える