Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.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
Python (Django)如何制作这个注册模型和注册视图?_Python_Django_Instagram - Fatal编程技术网

Python (Django)如何制作这个注册模型和注册视图?

Python (Django)如何制作这个注册模型和注册视图?,python,django,instagram,Python,Django,Instagram,我一直在尝试模仿Instagram注册,它可以是“电话”也可以是“电子邮件”。随附的图片显示了要求 以下是我创建的“帐户”Django应用程序的文件: models.py from django.db import models class Account(models.Model): email = models.EmailField(max_length = 254) password = models.Char

我一直在尝试模仿Instagram注册,它可以是“电话”也可以是“电子邮件”。随附的图片显示了要求

以下是我创建的“帐户”Django应用程序的文件:

models.py

from django.db                      import models

class Account(models.Model):
    email       = models.EmailField(max_length = 254)
    password    = models.CharField(max_length=700)
    fullname    = models.CharField(max_length=200)
    username    = models.CharField(max_length=200)
    phone       = models.CharField(max_length=100)
    created_at  = models.DateTimeField(auto_now_add=True)
    updated_at  = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'accounts'

    def __str__(self):
        return self.username + " " + self.fullname
views.py

import json
import bcrypt
import jwt

from django.views       import View
from django.http        import HttpResponse, JsonResponse
from django.db.models   import Q

from .models    import Account

class SignUpView(View):
    def post(self, request):
        data = json.loads(request.body)
        try:
            if Account.objects.filter(email=data['email']).exists():
                return JsonResponse({"message": "ALREADY EXIST"}, status=409)
            elif Account.objects.filter(email=data['phone']).exists():
                return JsonResponse({"message": "ALREADY EXIST"}, status=409)
            elif Account.objects.filter(username=data['username']).exists():
                return JsonResponse({"message": "ALREADY EXIST"}, status=409)

            hashed_pw = bcrypt.hashpw(data['password'].encode('utf-8'),bcrypt.gensalt()).decode()
            Account.objects.create(
                    email       = data['email'],
                    password    = hashed_pw,
                    fullname    = data['fullname'],
                    username    = data['username'],
                    phone       = data['phone'],

            )
            return JsonResponse({"message": "SUCCESS"}, status=200)

        except KeyError:
            return JsonResponse({"message": "INVALID_KEYS"}, status=400)

既然用户输入了电话号码或电子邮件,我如何让django区分电话和电子邮件,并将其放入正确的模型中

您可以使用验证电子邮件来验证输入是否为电子邮件,如果不是,则尝试验证电话样式

from django.core.validators import validate_email

try:
    validate_email(data['email_or_phone'])
    print('input is email')
except ValidationError:
    print('do phone validate')
文件: