字段错误位于/。无法解析关键字“textField”进入田野

发布于 2024-10-20 15:30:37 字数 1853 浏览 1 评论 0原文

我正在尝试为我的 django 应用程序执行一个过滤方法。我在数据库中有一些字段,例如用户名、职称、员工 ID 等。现在我正在尝试进行过滤;下拉菜单显示所有字段(姓名、职务、员工 ID 等)和用于输入一些文本的文本区域。应根据下拉选项选择和文本区域中输入的数据来完成过滤。在执行此操作时,我收到此错误:FieldError at /filter/ 无法将关键字“textField”解析为字段。选项包括:contactNumber、dateOfJoin、designation、employeeID、id、project、userName 我将在此处粘贴我的 html 和views.py。请帮我解决这个问题。

<form action="http://10.1.0.90:8080/filter/" method="POST">
Filter By: 
<select name="choices" onsubmit="document.forms[0].submit()" >
    <option value="">Select A Choice</option>
    <option value="Name">Name</option> 
    <option value="Designation" >Designation</option>
    <option value="EmployeeID" >EmployeeID</option>
    <option value="Project" >Project</option>
    <option value="Date Of Join" >Date Of Join</option> 
</select>
<input type="text" name="textField">
<input type="submit" value="Go">
</form>

{%for data in newData%}
{{ data.userName}}<br>
{%endfor%} 

这是我的 VIEWS.PY

def filter(request):     
    val2=request.POST.get('choices')
    val3=request.POST.get('textField')
    print val2,val3
    newData = EmployeeDetails.objects.filter(choices=request.POST.get('choices'),textField=request.POST.get('textField'))
    return render_to_response('filter.html',{'newData':newData,'val2':val2,'val3':val3})    

MODELS.PY

class EmployeeDetails(models.Model):
    userName = models.CharField(max_length=200)
    designation = models.CharField(max_length=200)
    employeeID = models.IntegerField()
    contactNumber = models.CharField(max_length=200)
    project = models.CharField(max_length=200)
    dateOfJoin=models.TextField()

我对 Django 很陌生。请帮我解决这个问题

I am trying to do a filter method for my django application. I have some fields in db like, userName,designation,employeeID etc . Now i am trying to do a filtering; where there is drop down shows all the fields (name, designation, employeeID etc) and a text area for inputing some text. The filtering should be done based on the drop down choice selection and the data entered in the text area. While doing this i got this error: FieldError at /filter/
Cannot resolve keyword 'textField' into field. Choices are: contactNumber, dateOfJoin, designation, employeeID, id, project, userName
I will paste my html and views.py here. Please help me to solve this.

<form action="http://10.1.0.90:8080/filter/" method="POST">
Filter By: 
<select name="choices" onsubmit="document.forms[0].submit()" >
    <option value="">Select A Choice</option>
    <option value="Name">Name</option> 
    <option value="Designation" >Designation</option>
    <option value="EmployeeID" >EmployeeID</option>
    <option value="Project" >Project</option>
    <option value="Date Of Join" >Date Of Join</option> 
</select>
<input type="text" name="textField">
<input type="submit" value="Go">
</form>

{%for data in newData%}
{{ data.userName}}<br>
{%endfor%} 

THIS is my VIEWS.PY

def filter(request):     
    val2=request.POST.get('choices')
    val3=request.POST.get('textField')
    print val2,val3
    newData = EmployeeDetails.objects.filter(choices=request.POST.get('choices'),textField=request.POST.get('textField'))
    return render_to_response('filter.html',{'newData':newData,'val2':val2,'val3':val3})    

MODELS.PY

class EmployeeDetails(models.Model):
    userName = models.CharField(max_length=200)
    designation = models.CharField(max_length=200)
    employeeID = models.IntegerField()
    contactNumber = models.CharField(max_length=200)
    project = models.CharField(max_length=200)
    dateOfJoin=models.TextField()

I am quite new to Django. Please help me to solve this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温暖的光 2024-10-27 15:30:37

为什么您期望 django 能够神奇地将字段名称映射到您在模板中指定的名称?例如,您的模型中没有任何内容称为您在

我发现这个错误非常不言自明:

/filter/ 处的 FieldError 无法解析
将关键字“textField”放入字段中。
选项有:联系号码、
加入日期、职称、员工 ID、
id、项目、用户名

您的模型中没有 textField 字段,但您正在尝试对其进行过滤。

想象一下 SELECT textField FROM mytable - textField 不存在。

...以及一个用于输入一些内容的文本区域
文本。


至于解决你的问题,你有几个。您正在尝试对选项中指定的特定字段进行查找,但选项和您的字段之间没有关系。

为了让您自己轻松完成此操作,至少将 标记的值设置为字段名称。

<select name="choices" onsubmit="document.forms[0].submit()" >
    <option value="">Select A Choice</option>
    <option value="userName">Name</option> 
    <option value="designation" >Designation</option>
    <option value="employeeID" >EmployeeID</option>
    <option value="project" >Project</option>
    <option value="dateOfJoin" >Date Of Join</option> 
</select>

if request.GET.get('choices'):
    # prevent query on field ''
    EmployeeDetails.objects.filter(**{request.GET.get('choices'): \
          request.GET.get('textField')})

Why are you expecting django to be able to magically map a field name to the names you specify in your template? For example, there's nothing in your model called any of the items you list in your <select> options.

I find the error very self explanatory:

FieldError at /filter/ Cannot resolve
keyword 'textField' into field.
Choices are: contactNumber,
dateOfJoin, designation, employeeID,
id, project, userName

You don't have a textField field in your model, yet you are trying to filter against it.

Imagine SELECT textField FROM mytable -- textField just doesn't exist.

... and a text area for inputing some
text.


As for fixing your problem, you have several. You are trying to do a lookup on a specific field specified in choices, but there is no relationship between Choices and your fields.

To make this easy on yourself, at least set the values for your <option> tags as the field names.

<select name="choices" onsubmit="document.forms[0].submit()" >
    <option value="">Select A Choice</option>
    <option value="userName">Name</option> 
    <option value="designation" >Designation</option>
    <option value="employeeID" >EmployeeID</option>
    <option value="project" >Project</option>
    <option value="dateOfJoin" >Date Of Join</option> 
</select>

if request.GET.get('choices'):
    # prevent query on field ''
    EmployeeDetails.objects.filter(**{request.GET.get('choices'): \
          request.GET.get('textField')})
佼人 2024-10-27 15:30:37

如果我明白你想要做什么,那么你使用过滤器的方式是错误的。要使用给定查询(val3)尝试过滤动态发布字段(val2)。

kwargs = {}
kwargs[val2] = val3

newData = EmployeeDetails.objects.filter(**kwargs)

您代码中的选项值需要映射模型才能正常工作。

If i understand what you are trying to do, you are using filter the wrong way. To filter on a dynamic posted field(val2) with a given query(val3) try.

kwargs = {}
kwargs[val2] = val3

newData = EmployeeDetails.objects.filter(**kwargs)

the option values in you code would need to map the model for this to work.

小兔几 2024-10-27 15:30:37

如果您没有名为“textField”的字段,则无法过滤“EmployeeDetails”模型。您只能使用以下可用字段过滤“EmployeeDetails”模型:“contactNumber、dateOfJoin、designation、employeeID、id、project、userName”。

=)

If you don't have one field called "textField" you can't filter your "EmployeeDetails" models. You only can filter the "EmployeeDetails" models with available fields how: "contactNumber, dateOfJoin, designation, employeeID, id, project, userName".

=)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文