2013-02-12 477 views
8

我对django非常陌生,并且努力做一些非常简单的事情。 我有以下模型ModelFormdjango:在同一页面输入并显示输出结果

class Queries(models.Model): 
    user_id=models.CharField(max_length=200) 
    query=models.CharField(max_length=200) 

而且我向用户显示一个简单的形式,将做以下帮助:

  • 用户会问一个问题
  • 的问题将被处理(根据问题将产生一个数据库查询 )

  • 然后查询结果shou ld将显示在 相同页面的表格下方。

这是我的views.py看起来像:

from django.http import HttpResponse 
from django.shortcuts import get_object_or_404, render 
from basicapp.models import QueryForm 

def index(request): 
    form=MyForm() 
    real_form=form.getForm(request) 
    response=form.response 
    return render(request,'basicapp/index.html',{ 
     'form': real_form, 
     'response':response, 
    }) 
class MyForm: 
    response='' 
    def getForm(self,request): 
     form = QueryForm(request.POST) 
     if form.is_valid(): 
      response=form.cleaned_data['query'] 
      form.save() 
     return form 

现在我想简单的东西,我是把形式的查询字段中的值,并试图将其发送回该页面;到目前为止我失败了。 这是index.html的:

<form action=" " method="post">{% csrf_token %} 
{{ form }} 
<p>{{response}}</p> 
<input type="submit" value="Submit" /> 
</form> 

如果我能做到这一点,我想查询充塞不会是tough.The形式是工作的罚款,该的数据都是得到保存在数据库中。表格提交后,中只有response字符串views.py无法检索。你能帮忙吗?

编辑: 尝试基于霍夫的回答中index.html如下:

<form id="myForm" action=" " method="get">{% csrf_token %} 
    {{ form }} 
    <input type="submit" value="Submit" /> 
</form> 
<div id="response"> 
</div> 
<script language="JavaScript"> 
    $(document).ready(function() { 
     $("#myForm").submit(function() { // catch the form's submit event 
      $.ajax({ // create an AJAX call... 
       data: $(this).serialize(), // get the form data 
       type: $(this).attr('GET'), 
       success: function(response) { // on success.. 
        $("#response").html(response); // update the DIV 
       } 
      }); 
      return false; 
     }); 
    }); 
</script> 

仍然没有运气:(

+2

你应该使用GET而不是POST。我认为你执行搜索 – catherine 2013-02-12 16:51:21

+0

谢谢:)编辑。 – 2013-02-12 17:12:37

回答

9

views.py

def index(request): 
    questions=None 
    if request.GET.get('search'): 
     search = request.GET.get('search') 
     questions = Queries.objects.filter(query__icontains=search) 

     name = request.GET.get('name') 
     query = Queries.object.create(query=search, user_id=name) 
     query.save() 

    return render(request, 'basicapp/index.html',{ 
     'questions': questions, 
    }) 

HTML

<form method="GET"> 
    Question: <input type="text" name="search"><br/> 
    Name: <input type="text" name="name"><br/> 
    <input type="submit" value="Submit" /> 
</form><br/><br/> 


{% for question in questions %} 
<p>{{question}}</p> 
{% endfor %} 
+0

你可以请解释一下:/困惑... – 2013-02-12 17:49:06

+0

整个代码:P应该所有这些驻留在views.py或HTML部分应该在index.html?如果那我的ModelForm正在使用? – 2013-02-12 17:56:49

+0

当用户提交问题“如果”条件将被触发并搜索该问题时,输出:它会显示包含您提交内容的问题列表 – catherine 2013-02-12 18:02:46

3

你需要的是一个异步后(阿贾克斯),这是很容易使用jQuery,见这个答案的完整解决方案:How to POST a django form with AJAX & jQuery

+0

请检查编辑的问题。我仍然不走运。 :( – 2013-02-12 17:13:29

1

继霍夫的回答......

添加网址属性Ajax调用:

$(document).ready(function() { 
    $("#myForm").submit(function() { // catch the form's submit event 
     $.ajax({ // create an AJAX call... 
      data: $(this).serialize(), // get the form data 
      type: $(this).attr('GET'), 
      url: '/URL-to-ajax-view/', 
      success: function(response) { // on success.. 
       $("#response").html(response); // update the DIV 
      } 
     }); 
     return false; 
    }); 
}); 

一些Ajax处理程序views.py:

# /URL-to-ajax-view/ 
def ajax_get_response(request): 
    if request.method == "GET" and request.is_ajax: 
     form = QueryForm(request.POST or None) 
     if form.is_valid(): 
      form.save() 
      return HttpResponse(form.response) 
    raise Http404 

试过类似的东西?

+0

现在就试试。 :)看起来很有前途... – 2013-02-12 18:07:53

+0

让我知道它是怎么回事:) – Ogre 2013-02-12 21:45:41

1
<input type="text" name="query" /> 
<input type="submit" name="submit" value="Submit" /> 

您可以检查是否提交表单或没有(我。e如果它是一个发布请求或不):

if 'submit' in request.POST: #you could use 'query' instead of 'submit' too 
    # do post related task 
    # add context variables to render post output 
    # add another context variable to indicate if it's a post 
    # Example: 
    context.update({'post_output': request.POST.get('query','')}) 
... 
return render(request, 'index.html', context) 
在模板

然后,检查是否上下文变量post_output存在,如果它确实显示输出:

{% if post_output %} 
    Output: {{ post_output }} 
{% endif %} 


总之,逻辑是:

  1. 检查相关request.POST字典是否存在或不在您的视图中。
  2. 如果密钥存在,那么它是一个post请求;添加帖子相关的上下文变量,并发布相关任务。
  3. 检查模板中是否有任何帖子相关的上下文变量可用,如果有,请显示帖子相关的输出。

如果不希望显示在页面帖子后,只需刷新输出,通过request对象模板,做这样的检查:

{% if request.POST.submit and post_output %} 
相关问题