简体   繁体   English

该视图未返回HttpResponse对象-Django和mongoengine

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

I'm trying to make a view for an auth user, I'm using django 1.6 and mongoengine, so far I don't know what might be the problem, this my forms.py: 我正在尝试为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')
            )
        )

And this is the class in my views.py: 这是我的views.py中的类:

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/')

I point the browser to the urls.py address: 我将浏览器指向urls.py地址:

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

But it keeps giving me this error, I know it should have an If statement for cases when request isn't POST, but it's already there, don't know if this is a syntax error or something. 但是它一直给我这个错误,我知道在请求不是POST的情况下应该有一个If语句,但是它已经存在了,不知道这是语法错误还是什么。

Anybody can shed some light on this? 任何人都可以对此有所了解吗?

Thanks in advance! 提前致谢!

EDIT 编辑

Updated full peer_signup method: 更新了完整的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/')

You form is invalid or user is already authenticated. 您的表格无效或用户已通过身份验证。 You should handle these cases as well as the GET request. 您应该处理这些情况以及GET请求。 So your view has to be like this: 因此,您的视图必须是这样的:

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})

Here is a simpler approach to your view: 这是一种更简单的视图方法:

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})

You do not want to do this: 不想这样做:

auth.login(request,peer_auth)

Unless you want to logout the currently logged-in user. 除非您要注销当前登录的用户。

You also don't want to do this password=str(peer.password) , because this may cause the wrong password to be used to authenticate the user. 您也不想这样做password=str(peer.password) ,因为这可能会导致使用错误的密码来认证用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM