python – Django – 修改简单搜索结果的外观

python – Django – 修改简单搜索结果的外观,第1张

概述我正在使用 django迈出第一步,目前我正在尝试使用简单的搜索解决方案为我的网站使用: http://julienphalip.com/post/2825034077/adding-search-to-a-django-site-in-a-snap 这是代码的样子: search.py import refrom django.db.models import Qdef normaliz 我正在使用 django迈出第一步,目前我正在尝试使用简单的搜索解决方案为我的网站使用: http://julienphalip.com/post/2825034077/adding-search-to-a-django-site-in-a-snap

这是代码的样子:

search.py

import refrom django.db.models import Qdef normalize_query(query_string,findterms=re.compile(r'"([^"]+)"|(\S+)').findall,normspace=re.compile(r'\s{2,}').sub):''' Splits the query string in invIDual keywords,getting rID of unecessary spaces    and grouPing quoted words together.    Example:    >>> normalize_query('  some random  words "with   quotes  " and   spaces')    ['some','random','words','with quotes','and','spaces']'''return [normspace(' ',(t[0] or t[1]).strip()) for t in findterms(query_string)] def get_query(query_string,search_fIElds):''' Returns a query,that is a combination of Q objects. That combination    aims to search keywords within a model by testing the given search fIElds.'''query = None # query to search for every search term        terms = normalize_query(query_string)for term in terms:    or_query = None # query to search for a given term in each fIEld    for fIEld_name in search_fIElds:        q = Q(**{"%s__icontains" % fIEld_name: term})        if or_query is None:            or_query = q        else:            or_query = or_query | q    if query is None:        query = or_query    else:        query = query & or_queryreturn query

vIEws.py

from news.models import *from news.search import *from django.shortcuts import render_to_responsefrom django.template import RequestContextdef search(request):query_string = ''found_entrIEs = Nonesearch_fIElds=('text','Title',)if ('q' in request.GET) and request.GET['q'].strip():    query_string = request.GET['q']    entry_query = get_query(query_string,search_fIElds)    found_entrIEs = News.objects.filter(entry_query).order_by('-ID')return render_to_response('search/search.HTML',{ 'query_string': query_string,'found_entrIEs': found_entrIEs },context_instance=RequestContext(request))

models.py

STATUS_CHOICES = (              ('d','Draft'),('p','Published'),('w','Withdrawn'),)class News(models.Model):category = models.ManyToManyFIEld(category,verbose_name='KategorIE')Title = models.CharFIEld(max_length=255,verbose_name='Tytuł')slug = models.SlugFIEld(max_length=255,unique=True,verbose_name='Odnośnik')text = models.TextFIEld(verbose_name='Treść')date = models.DateTimeFIEld(verbose_name='Data dodania')author = models.ForeignKey(User,verbose_name='autor')status = models.CharFIEld(max_length=1,choices=STATUS_CHOICES,default='d')class Meta:    verbose_name = "Wiadomość"    verbose_name_plural = "Wiadomość"def __str__(self):    return self.Titledef __unicode__(self):    return self.Titledef get_absolute_url(self):    return '/news/' + self.slug + '/'

search.HTML

{% if found_entrIEs %}                <p>You searched for "{{ query_string }}".</p>                                <ul>                    {% for i in found_entrIEs %}                        <li><a href="{{ q.get_absolute_url }}">{{found_entrIEs }}</a></li>                    {% endfor %}                    </ul>            {% endif %}            {% if query_string and not found_entrIEs %}                <p>No results found.</p>            {% else %}                <p>Type a search query into the Box above,and press "submit" to search.</p>            {% endif %}

我想要做的是获得一个正确的搜索结果视图(搜索新闻模型,这应该返回几个文本的标题可能吗?)这是它看起来像atm:http://dl.dropbox.com/u/26827941/ScreenShot108.png(我不允许发布图像)

我花了至少几个小时试图找到一个合适的解决方案,我应该如何修改我的vIEws.py和search.HTML以获得正确的查看结果,你能帮我一点吗?

解决方法 这就是你的问题所在

{% for i in found_entrIEs %}                    <li><a href="{{ i.get_absolute_url }}">{{i}}</a></li>                {% endfor %}

当您只显示{{i}}时,将使用模型的__unicode__方法.

如果您想要显示更多内容,可以使用{{i.category}},{{i.Title}}等.

总结

以上是内存溢出为你收集整理的python – Django – 修改简单搜索结果的外观全部内容,希望文章能够帮你解决python – Django – 修改简单搜索结果的外观所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1192660.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存