【问题标题】:I have trouble finding the site我找不到网站
【发布时间】:2019-07-14 16:12:05
【问题描述】:

我在网站上搜索文章。过滤应按文章标题。 来自搜索的数据应该显示在模板中/我尝试使用 order_by 和过滤器过滤数据

views.py

def post_search(request):
    form = SearchForm()
    if 'query' in request.GET:
        form = SearchForm(request.GET)
        search = request.GET['username']


        if form.is_valid():
            cd = form.cleaned_data
            results = SearchQuerySet().models(Articles).filter(content=cd['query']).load_all()

            total_results = results.count()
    return render(request,
                  'news/posts.html',
                  {'form': form,
                   'search': search,
                   'cd': cd,
                   'results': results,
                   'total_results': total_results})

posts.html

   {% if "query" in request.GET %}
        <h1>Posts containing "{{ cd.query }}"</h1>
        <h3>Found {{ total_results }} result{{ total_results|pluralize }}</h3>
        {% for result in results %}
            {% with post=result.object %}
                <h4><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h4>
                {{ post.body|truncatewords:5 }}
            {% endwith %}
            {% empty %}
            <p>There are no results for your query.</p>
        {% endfor %}
        <p><a href="{% url 'post_search' %}">Search again</a></p>
    {% else %}
        <h1>Search for posts</h1>
        <form action="{% url 'post_search' %}" method="get">
            <h3>Search</h3><input  style=" border: 3px solid #4242A3; border-radius: 15px ; " type="text" name="search" value=" searchк "> <br>
            <input type="submit" value="Search">
        </form>
    {% endif %}

urls.py

path('search/', views.post_search, name='post_search'),

models.py

class Articles(models.Model):
    title = models.CharField(max_length= 200)
    post = models.TextField()
    date = models.DateTimeField()
    img = models.ImageField(upload_to='', default="default_value")
    tags = TaggableManager()
    article_like = models.IntegerField(default='0')
    article_dislike = models.IntegerField(default='0')
    view = models.IntegerField(default='0')


    def __str__(self):
        return self.title

【问题讨论】:

  • 如果'query' not in request.GET怎么办?
  • 什么也没发生。应该吗?
  • 自己思考。如果“查询”不存在,是否会分配“搜索”变量?

标签: python html django


【解决方案1】:

如果您尝试按文章标题搜索帖子,那么您可以像这样更改您的视图:

 def post_search(request):
    query = request.GET.get('search')
    if query:          
        results = Article.objects.filter(title__icontains=query).order_by('-date')

        total_results = results.count()
        return render(request,
              'news/posts.html',
              {                  
               'results': results,
               'query':query,
               'total_results': total_results})
    else:
          messages.info(request,'no results found for {}',format(query))

在你的模板中你可以这样做:

 <h1>Posts containing "{{ query }}"</h1>
    <h3>Found {{ total_results }} result{{ total_results|pluralize }}</h3>
    {% for result in results %}
        {% with post=result.object %}
            <h4><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h4>
            {{ post.body|truncatewords:5 }}
        {% endwith %}
        {% empty %}
        <p>There are no results for your {{query}}.</p>
    {% endfor %}
    <p><a href="{% url 'post_search' %}">Search again</a></p>
{% else %}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 2016-08-20
    • 1970-01-01
    • 2019-07-12
    • 2020-03-24
    相关资源
    最近更新 更多