字段错误位于/。无法解析关键字“textField”进入田野
我正在尝试为我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么您期望 django 能够神奇地将字段名称映射到您在模板中指定的名称?例如,您的模型中没有任何内容称为您在
我发现这个错误非常不言自明:
您的模型中没有
textField
字段,但您正在尝试对其进行过滤。想象一下
SELECT textField FROM mytable
-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:
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.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.如果我明白你想要做什么,那么你使用过滤器的方式是错误的。要使用给定查询(val3)尝试过滤动态发布字段(val2)。
您代码中的选项值需要映射模型才能正常工作。
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.
the option values in you code would need to map the model for this to work.
如果您没有名为“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".
=)