Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PythonDjango中,我想对if-else执行两条语句,即if-else,if-email是taked-show-message,if-employee-id是taked-show-message_Python_Django - Fatal编程技术网

在PythonDjango中,我想对if-else执行两条语句,即if-else,if-email是taked-show-message,if-employee-id是taked-show-message

在PythonDjango中,我想对if-else执行两条语句,即if-else,if-email是taked-show-message,if-employee-id是taked-show-message,python,django,Python,Django,我有一个html注册表单,我在其中显示是否存在用户名或电子邮件,如果存在,则显示用户名;如果存在电子邮件,则显示电子邮件,但问题是,即使我在html表单的电子邮件字段中发送电子邮件,也会显示用户名已被接收,但不是电子邮件我尝试了elif语句,但没有工作的用户名是采取是完美的工作,但不是电子邮件 如果有人知道,请帮忙 这是我的观点 def Register(request): try: if request.method == 'POST': use

我有一个html注册表单,我在其中显示是否存在用户名或电子邮件,如果存在,则显示用户名;如果存在电子邮件,则显示电子邮件,但问题是,即使我在html表单的电子邮件字段中发送电子邮件,也会显示用户名已被接收,但不是电子邮件我尝试了elif语句,但没有工作的用户名是采取是完美的工作,但不是电子邮件

如果有人知道,请帮忙

这是我的观点

def Register(request):
    try:
        if request.method == 'POST':
            username = request.POST.get('username')
            email = request.POST.get('email')
            password = request.POST.get('password')

        try:
            if User.objects.filter(username = username).first():
                messages.success(request, 'Username is taken.')
                return redirect('/register/')

            if User.objects.filter(email = email).first():
                messages.success(request, 'Email is taken.')
                return redirect('/register/')
            
            user_obj = User(username = username , email = email)
            user_obj.set_password(password)
            user_obj.save()
    
            profile_obj = Profile.objects.create(user = user_obj )
            profile_obj.save()
            return redirect('/login/')

        except Exception as e:
            print(e)

    except Exception as e:
            print(e)

    return render(request , 'register.html')

由于django已经将unique设置为username,所以同样地,您可以将电子邮件设置为在模型本身中唯一

User._meta.get_field('email')._unique = True

然后,您可以创建一个表单,并检查其是否有效,以及用户名或电子邮件是否有重复条目,它将显示其已被使用。

怎么办。首先返回。它对电子邮件字段有效吗?您是否为电子邮件条件检查提供相同的用户名?只需检查此行am am am为电子邮件和用户名提供两个条件仅第一条语句有效,即username if User.objects.filter(username=username.first():messages.success(请求“使用用户名”。)如果User.objects.filter(email=email.first():messages.success(请求“接收电子邮件”),则返回重定向(“/register/”)返回重定向(“/register/”)是的,用户名和电子邮件的返回都不同,它们只是返回redirect@irfan waniPls如果User.objects.filter(email=email).exists()并检查,则替换条件。我应该将此语句放在哪里?在models.py文件中否实际上我的要求不是这只是检查我的代码