没有帖子与给定查询匹配

时间:2019-02-27 17:23:41

标签: django django-templates django-views

我正在使用Django版本2。

我正在使用PostgreSQL数据库开发博客网络应用。

我正在尝试向Web应用程序添加搜索功能,但是当我打开URL(http://localhost:8000/search/)进行搜索时,出现以下错误。

找不到页面(404) 请求方法:GET 要求网址:http://localhost:8000/search/ 提出者:blog.views.post_detail 没有帖子匹配给定查询。

这是blog / urls.py

from django.contrib.postgres.search import SearchVector
from .forms import CommentForm, SearchForm
from .models import Post, Comment
from django.shortcuts import render, get_object_or_404

from django.urls import path
from . import views 


urlpatterns = [
    path('', views.PostListView.as_view(), name='home'),
    path('<slug:post>/', views.post_detail, name='post_detail'),
    path('<int:post_id>/share', views.post_share, name='share'),
    path('search/', views.post_search, name='post_search'),
]

这是views.py

def post_detail(request, post):
    post = get_object_or_404(Post, slug=post)
    comments = post.comments.filter(active=True)

    new_comment = None

    if request.method == 'POST':
        comment_form = CommentForm(data=request.POST)
        if comment_form.is_valid():
            new_comment = comment_form.save(commit=False)
            new_comment.post = post
            new_comment.save()

    else:
        comment_form = CommentForm()
    return render(request, 'detail.html', {'post': post, 
        'comments': comments, 'new_comment': 
        new_comment,'comment_form': comment_form})

def post_search(request):
    form = SearchForm()
    query = None
    results = []
    if 'query' in request.GET:
        form = SearchForm(request.GET)
        if form.is_valid():
            query = form.cleaned_data['query']
            results = Post.objects.annotate(
                search=SearchVector('title','body'),
                ).filter(search=query)
    return render(request, 'search.html', 
        {'form':form,'query':query,'results':results})

这是模板文件。

对于search.html-(进行查询的页面)

{% extends 'base.html' %} 

{% block title %}Search{% endblock title %}
    
{% block page_title %}Search Posts{% endblock page_title %}
    
{% block content %}


{% if query %}
<h1>Posts containing "{{ query }}"</h1>
<h3>
    
    {% with results.count as total_results %}

        Found {{ total_results }} result{{ total_results|pluralize }}
        
    {% endwith %}
        
</h3>

{% for post in results %}

    <h4><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h4>
    {{ post.body|truncatewords:5 }}
{% empty %}
    <p>There is 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="."  method="GET">
        {{ form.as_p }}
        <input type="submit" value="Search">
    </form>
        
{% endif %}
    

{% endblock content %}
 


  
        

用于post_detail的html

{% extends 'base.html' %}
{% load blog_tags %}
{% load crispy_forms_tags %}
{% load profanity %}
{% block title %}{{post.title}}{% endblock title %}

{% block navlink %}

<nav>
    <ul>
        <li class="current"><a href="{% url 'home' %}">Home</a></li>
        <li><a href="#">About</a></li>
        <li ><a href="#">Services</a></li>
    </ul>
</nav>
  
{% endblock navlink %}
    
{% block page_title %}{{post.title}}{% endblock page_title %}
    
{% block content %}
{{post.body|markdown}}

<p><a href= "{% url 'share' post.id  %}">Share this post via email</a></p>


{% with comments.count as total_comments %}

    <h2>{{ total_comments }} comment{{ total_comments|pluralize }} </h2>
    
{% endwith %}

<div class="container">
    {% for comment in comments %}
    <div class="comment">
        <p>Comment {{ forloop.counter }} by <em>{{ comment.name }}</em> - {{comment.created}} </p>
        {{ comment.body|censor|linebreaks }}
    </div>
    {% empty %}
    
    <p>There are no comments yet.</p>
    
    {% endfor %}

    {% if new_comment %}
        <h2>Your comment has been added</h2>
        
    {% else %}
        
    <h2>Add a new comment</h2>
    <form  method="post">{% csrf_token %}
        {{comment_form|crispy}}
        <input class="button_1" type="submit" value="Add comment">
    </form>

            
    {% endif %}    

</div> 

    
       

{% endblock content %}


 


  
        

1 个答案:

答案 0 :(得分:2)

这可能是由于search实际上与 slug:post 相匹配的原因之一。解决您问题的最简单方法之一是重新排列URL并确保之前放置/search。就像关注

urlpatterns = [
    path('', views.PostListView.as_view(), name='home'),
    path('search/', views.post_search, name='post_search'),
    path('<slug:post>/', views.post_detail, name='post_detail'),
    path('<int:post_id>/share', views.post_share, name='share')

]

我建议进一步开发,只要它需要在URL中添加slug,我们就需要确保在它之前有一些前缀。