【问题标题】:django error in url generated by form, url is not matching. getting unwanted comma between my query search表单生成的 url 中的 django 错误,url 不匹配。在我的查询搜索之间出现不需要的逗号
【发布时间】:2019-02-20 20:18:41
【问题描述】:

我想提交一个表单,然后我想调用一个特定的视图,但我的 url 不匹配“,”介于两者之间。

我的表格:

<form action={% url "blog:post_search" %}, method="get">
<input type="text" name="query">
<input type="submit" value="search">
</form>

我的看法:

def post_search(request):
    if 'query' in request.GET:
        search_query = SearchQuery(request.GET['query'])
        search_vector = SearchVector('title', 'body')
        result = Post.objects.annotate(search=search_vector,
                    rank=SearchRank(search_vector,search_query)
                    ).filter(search=search_query).order_by('-rank')
        return render(request,'blog/post/search.html',{'query':search_query,
                                                   'result':result})

我的网址:

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

引发错误:

找不到页面 (404) 请求方法:GET 请求
网址:http://127.0.0.1:8000/blog/search/,?query=confused 使用
在 myproject.urls 中定义的 URLconf,Django 尝试了这些 URL 模式,
按此顺序:admin/ blog/ [name='post_list'] blog/
标签// [name='post_list_by_tag'] 博客/
//// [name='post_details']
博客/ /share/ [name='post_share'] 博客/ 搜索/
[name='post_search'] sitemap.xml
[name='django.contrib.sitemaps.views.sitemap'] 当前路径,
blog/search/,, 与这些都不匹配。

【问题讨论】:

    标签: django python-3.x


    【解决方案1】:

    因为您在表单标签中放置了一个逗号。 HTML 属性应该用引号引起来,而不是用逗号分隔。

    <form action="{% url "blog:post_search" %}" method="get">
    

    【讨论】:

      猜你喜欢
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 2013-07-22
      • 2021-12-26
      相关资源
      最近更新 更多