用Django进行干草堆搜索

时间:2014-08-21 14:02:33

标签: python django django-models django-haystack

我设法让干草堆搜索正常工作,并且大部分时间都在努力工作,唯一困扰我的是,当我进入搜索页面时,有一个“SearchIn”选项,有一个标签为“documents”的复选框,无论是否检查,结果都相同,我想知道是否有人知道如何删除它,我知道这可能是一个简单的修复,但我可以'似乎弄清楚了。如果您有任何其他问题或疑虑,请随时发表评论。先感谢您。

models.py:

class Course(models.Model):

    subject = models.CharField(max_length = 255)

     def __unicode__(self):

       return self.subject

class Document(models.Model):

    file = models.FileField(upload_to='documents/%Y/%m/%d')
    subject_number = models.PositiveIntegerField(max_length=999,default=False)
    subject_name = models.CharField(max_length=255)
    title = models.CharField(max_length=255)
    course = models.ForeignKey(Course)
    description = models.TextField(default=None)
    pub_date = models.DateField(default = datetime.date.today())

    def __unicode__(self):

      return self.title

    def get_absolute_url(self):

      return "/main/get/%i/" % self.id

search_indexes.py:


import datetime
from haystack import indexes
from notes_app.models import Document, Course

class DocumentIndex(indexes.SearchIndex, indexes.Indexable):

  text = indexes.CharField(document=True, use_template=True)
  course = indexes.CharField(model_attr="course")
  subject_name = indexes.CharField(model_attr="subject_name")
  subject_number = indexes.IntegerField(model_attr="subject_number")
  description = indexes.CharField(model_attr="description")
  title = indexes.CharField(model_attr="title")
  pub_date = indexes.DateTimeField(model_attr="pub_date")


  def get_model(self):

    return Document

  def index_queryset(self, using=None):

    #Used when entire index for model is updated.
    return self.get_model().objects.all()

document_text.txt:


{{ object.course }}
{{ object.subject_name }}
{{ object.subject_number }}
{{ object.title }}
{{ object.description }}
{{ object.pub_date }}
{{ object.body }}

search.html:


<form method="get" action=".">
        <table>
            {{ form.as_table }}
            <tr>
                <td>&nbsp;</td>
                <td>
                    <input type="submit" value="Search">
                </td>
            </tr>
        </table>

        {% if query %}
            <h3>Results</h3>
            <table cellspacing="30">
              <tr>
                 <td>Course</td>
                 <td>Subject Name</td>
                 <td>Subject Number</td>
                 <td>Title</td>
                 <td>Description</td>
                 <td>Date Uploaded</td>
              </tr>
            {% for result in page.object_list %}


              <tr>
                <td align = "center"><a href="{{ result.object.get_absolute_url }}">{{    result.object.course }}</a></td>
                <td align = "center"><a href="{{ result.object.get_absolute_url }}">{{ result.object.subject_name }}</a></td>
                <td align = "center"><a href="{{ result.object.get_absolute_url }}">{{ result.object.subject_number }}</a></td>
                <td align = "center"><a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a></td>
                <td align = "center"><a href="{{ result.object.get_absolute_url }}">{{ result.object.description }}</a></td>
                <td align = "center"><a href="{{ result.object.get_absolute_url }}">{{ result.object.pub_date }}</a></td>
              </tr>

            {% empty %}
                <p>No results found.</p>
            {% endfor %}
          </table>
          </form>

1 个答案:

答案 0 :(得分:0)

我通过用以下内容替换当前表单来解决这个问题:

<form method="get" action="/search/">
      <input type="text" name="q">
      <button type="submit">Search</button>
    # Some Code
</form>