我找不到网站

时间:2019-07-14 16:12:05

标签: python html django

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

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

1 个答案:

答案 0 :(得分:0)

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

 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 %}