【问题标题】:Unable redirect page on form submission in django在 django 中提交表单时无法重定向页面
【发布时间】:2017-08-11 22:02:56
【问题描述】:

我正在尝试在 django 中实现文档搜索引擎。主页有一个文本框,用户应该在其中输入查询。在输入查询时,用户将被重定向到一个页面,该页面将包含与其查询相关的文档标题。但是,我无法在提交查询时重定向到新页面。我的应用名为search,这是打开http://127.0.0.1:8000/search/时加载的页面

index.html

{% load static %}

<html>
<head>
<link rel="stylesheet" type="text/css" href="{% static 'search/style.css' %}" />
<title>muGoogle</title>
</head>
<body>
<div class = "container">
<h4 class = 'mugoogle'>muGoogle</h4>
<form  class="form-wrapper" method = "GET" action = "{% url 'doc_scorer'%}">
    <input type="text" name = 'query' class="textbox rounded" placeholder="Search docs related to..." required>
    <input type="submit" class="button blue" value="Submit">
</form>
</div>
</body>
</html>

urls.py文件:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index),
    url(r'^search$', views.search),
    url(r'^index$', views.index),
    url(r'', views.doc_scorer, name="doc_scorer"),
]

views.py 文件

from django.template import Context, loader
from django.shortcuts import render, render_to_response
from django.http import Http404, HttpResponse, HttpResponseRedirect
import os
from other_code import some_function

def index(request):
    template = loader.get_template("search/index.html")
    return HttpResponse(template.render())

def search(request):
    return HttpResponse("search is under construction")

def doc_scorer(request):
    query = request.GET.get('query')
    context = {'query':some_function(query)}
    return render_to_response('query_results.html', context)

我想将结果重定向到这个页面。

query_results.html

{{ context['query'] }}

任何想法我哪里出错了?

【问题讨论】:

  • 您是否遇到错误或其他问题?
  • @nik_m 没有错误。没有重定向。提交时,只有 url 没有其他变化
  • doc_scorer 真的被调用了吗?在query = request.GET.....下面放一个print(query)并检查它是否真的被打印出来了。
  • 增强功能是您可以在文本输入字段上实现 ajax 以显示可能的关键字,因为它是一个搜索引擎!

标签: python html django redirect


【解决方案1】:

您的 index 视图和 doc_scorer 视图似乎具有相同的 url(在这两种情况下都是“/”)。因此,如果表单将其数据发送到“/”,它将由索引视图处理,该索引视图在 url 模式列表中排在首位。您应该更改 doc_scorer 视图的 url conf:

urlpatterns = [...
    url('^doc_scorer/$', views.doc_scorer, name='doc_scorer'),
]

【讨论】:

  • 好吧,这是有道理的。另一个快速的问题。在提交查询时,我希望重定向的 url 采用http://127.0.0.1:8000/search/?query= 的形式。我如何做到这一点
  • 你的表单action指向{% url 'doc_scorer' %},假设这被解析为localhost:8000/doc_scorer/'. Since your form's method`是“GET”,浏览器会自动将任何提交的数据作为url的一部分传递并将调用网址localhost:8000/doc_scorer/?query=&lt;whatever&gt;。所以,我想你不需要做任何特别的事情来实现你的目标......请注意你的模板中的表单没有提交到'/search/'。
【解决方案2】:

可能url (r '^ $', views.index)会优先执行。 尝试更改索引视图。像这样。

def index(request):
    if not request.GET.get('query'):
        return render_to_response('search/index.html', {})
    # search execution
    query = request.GET.get('query')
    context = {'query':some_function(query)}
    return render_to_response('query_results.html', context)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    相关资源
    最近更新 更多