Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.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 - Fatal编程技术网

Python 如何在Django中将表单输入与数据库值进行比较?

Python 如何在Django中将表单输入与数据库值进行比较?,python,django,Python,Django,我有一个表单来输入一个用户id,我想将这个id与数据库值(usrId)进行比较 forms.py from django import forms from .models import UserInfo class NameForm(forms.Form): your_id = forms.CharField(label='Your id', max_length=100) def clean(self): cleaned_data = super(NameForm, se

我有一个表单来输入一个用户id,我想将这个id与数据库值(usrId)进行比较

forms.py

from django import forms
from .models import UserInfo

class NameForm(forms.Form):
  your_id = forms.CharField(label='Your id', max_length=100)
  def clean(self):
     cleaned_data = super(NameForm, self).clean()
     your_id = cleaned_data.get("your_id")
     p = UserInfo.objects.all()
     if your_id:
        for i in p:
           if i.usrId not in your_id:
              raise forms.ValidationError(
                   "User not exist."
                  )
class UserInfo(models.Model):
   name = models.CharField(max_length=200)
   usrId = models.CharField(max_length=200)
   age = models.CharField(max_length=200)
   poste = models.CharField(max_length=200)
   date1 = models.DateTimeField('date of recruitment')
   def __str__(self):              # __unicode__ on Python 2
      return self.name
    # if this is a POST request we need to process the form data
if request.method == 'POST':
    # create a form instance and populate it with data from the request:
    form = NameForm(request.POST)
    # check whether it's valid:
    if form.is_valid():
        # process the data in form.cleaned_data as required

        # ...
        # redirect to a new URL:
        return generate_pdf(request, Type_id)

# if a GET (or any other method) we'll create a blank form
else:
        form = NameForm()

return render(request, 'rh/detail.html', {'form': form, 'type_id': Type_id})
当我这样做时,什么都不会发生,我得到的
用户不存在。
对于任何值

型号.py

from django import forms
from .models import UserInfo

class NameForm(forms.Form):
  your_id = forms.CharField(label='Your id', max_length=100)
  def clean(self):
     cleaned_data = super(NameForm, self).clean()
     your_id = cleaned_data.get("your_id")
     p = UserInfo.objects.all()
     if your_id:
        for i in p:
           if i.usrId not in your_id:
              raise forms.ValidationError(
                   "User not exist."
                  )
class UserInfo(models.Model):
   name = models.CharField(max_length=200)
   usrId = models.CharField(max_length=200)
   age = models.CharField(max_length=200)
   poste = models.CharField(max_length=200)
   date1 = models.DateTimeField('date of recruitment')
   def __str__(self):              # __unicode__ on Python 2
      return self.name
    # if this is a POST request we need to process the form data
if request.method == 'POST':
    # create a form instance and populate it with data from the request:
    form = NameForm(request.POST)
    # check whether it's valid:
    if form.is_valid():
        # process the data in form.cleaned_data as required

        # ...
        # redirect to a new URL:
        return generate_pdf(request, Type_id)

# if a GET (or any other method) we'll create a blank form
else:
        form = NameForm()

return render(request, 'rh/detail.html', {'form': form, 'type_id': Type_id})
视图.py

from django import forms
from .models import UserInfo

class NameForm(forms.Form):
  your_id = forms.CharField(label='Your id', max_length=100)
  def clean(self):
     cleaned_data = super(NameForm, self).clean()
     your_id = cleaned_data.get("your_id")
     p = UserInfo.objects.all()
     if your_id:
        for i in p:
           if i.usrId not in your_id:
              raise forms.ValidationError(
                   "User not exist."
                  )
class UserInfo(models.Model):
   name = models.CharField(max_length=200)
   usrId = models.CharField(max_length=200)
   age = models.CharField(max_length=200)
   poste = models.CharField(max_length=200)
   date1 = models.DateTimeField('date of recruitment')
   def __str__(self):              # __unicode__ on Python 2
      return self.name
    # if this is a POST request we need to process the form data
if request.method == 'POST':
    # create a form instance and populate it with data from the request:
    form = NameForm(request.POST)
    # check whether it's valid:
    if form.is_valid():
        # process the data in form.cleaned_data as required

        # ...
        # redirect to a new URL:
        return generate_pdf(request, Type_id)

# if a GET (or any other method) we'll create a blank form
else:
        form = NameForm()

return render(request, 'rh/detail.html', {'form': form, 'type_id': Type_id})

假设您尝试匹配的用户id确实存在(记录该id并手动查询数据库以确保)。您的代码应更改如下:

try:
    p = UserInfo.objects.get(id=your_id)
except UserInfo.DoesNotExist:
    raise forms.ValidationError("User not exist.")
这段代码更短,效率更高(不像当前版本那样获取所有用户对象)