我正在尝试创建一个搜索页面,该页面使用与不同数据段对应的三个字段来搜索模型对象。下面是我的代码:
在模型.py在
class Schedules(models.Model):
course_name = models.CharField(max_length=128, choices=COURSE_NAME_CHOICES, default='a-plus')
start_date = models.DateField(auto_now=False, auto_now_add=False, default=datetime.date.today)
instructor = models.CharField(max_length=128, choices=INSTRUCTOR_CHOICES, default='adewale')
在视图.py在
^{pr2}$搜索_日程表.html在
{% block main_content %}
<form method="post" action="">
{% csrf_token %}
<label for="course_name_search">Course Name:</label>
<input type="text" name="course_name_search" id="course_name_search">
<label for="start_date_search">Start Date:</label>
<input type="datetime" name="start_date_search" id="start_date_search">
<label for="instructor_search">Instructor:</label>
<input type="text" name="instructor_search" id="instructor_search"><br>
<input type="submit" name="submit">
</form>
<div id="result_panel">
{% if table %}
{% render_table table %}
{% else %}
{% if no_results %}
No results returned for <q>{{ no_results }}</q>
{% else %}
Please enter a search
{% endif %}
{% endif %}
{% endblock %}
由于某些原因,如果我在“课程名称”字段或“讲师”字段中键入,则搜索将有效,但如果我键入多个字段,则搜索将不起作用。由于某些原因,无论我如何键入日期,“开始日期”字段都不会起作用。有人能帮我正确地键入代码吗?谢谢您。在
您可以尝试向下搜索筛选方法,以最小化if语句:
但是这里有一些警告,以及一些帮助您解决开始日期问题的方法,因为您是直接读取POST数据,所以在查询之前,您需要将您的start_date_搜索转换为实际日期。在
另一件对你有帮助的事情是最好使用GET而不是POST,然后你可以在模板中读取它来填充你现在填充到“no_results”中的所选值。在
如果有用的话试试这个。在
相关问题 更多 >
编程相关推荐