该视图没有 return HttpResponse 对象 - Django 和 mongoengine

The view didn't return an HttpResponse object - Django and mongoengine

我正在尝试为 auth 用户创建视图,我正在使用 django 1.6 和 mongoengine,到目前为止我不知道可能是什么问题,这是我的 forms.py:

class FormSignup(forms.Form):   
    #first_name = forms.CharField(label = "Nombres")
    #last_name = forms.CharField(label = "Apellidos")
    username = forms.CharField(label= "Nombre de usuario")
    email = forms.EmailField(label = "Correo")
    email2 = forms.EmailField(label = "Ingresa el correo nuevamente")
    password = forms.CharField(widget=forms.PasswordInput() )
    birthday = forms.DateField(label = "Fecha Nacimiento", widget=SelectDateWidget())
    sex = forms.ChoiceField(widget=forms.RadioSelect, choices = SEXUAL_GENRES, label = "Sexo")

    def __init__(self, *args, **kwargs):
        super(FormSignup,self).__init__(*args,**kwargs)
        self.helper = FormHelper()
        self.helper.form_action = '/peer/signup/'       
        self.helper.layout = Layout(
            Fieldset(
                'Ingresa tus datos',
                #Row(
                #   Field('first_name', wrapper_class="large-6 columns"),
                #   Field('last_name', wrapper_class="large-6 columns"),                    
                #   ),
                Row(
                    'username'
                ),
                Row(
                    'email'
                    ),
                Row(
                    'email2'
                    ),
                Row(
                    'password'
                    ),
                Row(
                    MultiWidgetField('birthday',attrs={'class':'large-4'})
                    ),
                Row(
                    'sex'
                    )
            ),
            ButtonHolder(
                Submit('submit','Submit',css_class='button white')
            )
        )

这是我 views.py 中的 class:

def peer_signup(request):
    if not request.user.is_authenticated() and request.method == 'POST':
        form_signup = FormSignup(request.POST)
        if form_signup.is_valid():          
            nombreusuario = form_signup.cleaned_data['username']
            #verificar validacion email == email2
            email = form_signup.cleaned_data['email']
            password = form_signup.cleaned_data['password']
            birthday = form_signup.cleaned_data['birthday']
            sex = form_signup.cleaned_data['sex']

            peer = service_save_peer(username=nombreusuario,email=email,birth_date=birthday,first_name=u'',last_name=u'',password=password,sex=sex)
            if peer:
                peer_auth = service_authenticate_peer(peer=str('chavez'),password=str(peer.password))
                auth.login(request,peer_auth)       
            return HttpResponse('/peer/signup/')

我将浏览器指向 urls.py 地址:

    url(r'^peer/signup/$' , peer_signup),

但它一直给我这个错误,我知道它应该有一个 If 语句来处理请求不是 POST 的情况,但它已经存在了,不知道这是语法错误还是东西。

有人可以解释一下吗?

提前致谢!

编辑

更新了完整的 peer_signup 方法:

def peer_signup(request):
    if not request.user.is_authenticated() and request.method == 'POST':
        form_signup = FormSignup(request.POST)
        if form_signup.is_valid():          
            nombreusuario = form_signup.cleaned_data['username']
            #verificar validacion email == email2
            email = form_signup.cleaned_data['email']
            password = form_signup.cleaned_data['password']
            birthday = form_signup.cleaned_data['birthday']
            sex = form_signup.cleaned_data['sex']

            peer = service_save_peer(username=nombreusuario,email=email,birth_date=birthday,first_name=u'',last_name=u'',password=password,sex=sex)
        else:
            form_signup = FormSignup()
        return render(request, 'peer_signup.html', {'form': form_signup})
        if peer:
            peer_auth = service_authenticate_peer(peer=str('chavez'),password=str(peer.password))
            auth.login(request,peer_auth)       
        return HttpResponse('/peer/signup/')

您的表单无效或用户已通过身份验证。您应该处理这些情况以及 GET 请求。所以你的观点必须是这样的:

def peer_signup(request):
    if not request.user.is_authenticated() and request.method == 'POST':
        form_signup = FormSignup(request.POST)
        if form_signup.is_valid():          
            nombreusuario = form_signup.cleaned_data['username']
            #verificar validacion email == email2
            email = form_signup.cleaned_data['email']
            password = form_signup.cleaned_data['password']
            birthday = form_signup.cleaned_data['birthday']
            sex = form_signup.cleaned_data['sex']

            peer = service_save_peer(username=nombreusuario,email=email,
                                     birth_date=birthday,first_name=u'',
                                     last_name=u'',password=password,sex=sex)
            if peer:
                peer_auth = service_authenticate_peer(peer=str('chavez'),
                                                  password=str(peer.password))
                auth.login(request,peer_auth)       
        return HttpResponse('/peer/signup/')
    else:
        form_signup = FormSignup()
    return render(request, 'peer_signup.html', {'form': form_signup})

以下是您的观点的更简单方法:

from django.contrib.auth.decorators import login_required

@login_required
def peer_signup(request):
    form = FormSignup(request.POST or None)
    if form.is_valid():
        nombreusuario = form.cleaned_data['username']

        email = form.cleaned_data['email']
        password = form.cleaned_data['password']
        birthday = form.cleaned_data['birthday']
        sex = form.cleaned_data['sex']
        peer = service_save_peer(username=nombreusuario,email=email,
                                 birth_date=birthday,first_name=u'',
                                 last_name=u'',password=password,sex=sex)

        if peer:
            service_authenticate_peer(peer=str('chavez'),
                                      password=peer.password)
            return redirect('/thank-you-page')
    return render(request, 'peer_signup.html', {'form': form})

想这样做:

auth.login(request,peer_auth)

除非您想注销当前登录的用户。

您也不希望这样做password=str(peer.password),因为这可能会导致使用错误的密码来验证用户。