django:在同一页面中输入并显示输出

编程入门 行业动态 更新时间:2024-10-26 20:27:25
本文介绍了django:在同一页面中输入并显示输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我对django很新,并且努力地做一些非常简单的事情。 我有一个 ModelForm 为以下型号:

类查询(models.Model): user_id = models.CharField(max_length = 200) query = models.CharField(max_length = 200)

我向用户展示了一个简单的表单,可以帮助您执行以下操作:

  • 用户将提出问题
  • 该问题将被处理(根据问题将生成一个数据库查询)

  • 然后查询结果应该显示在相同页面的下方。

这是我的views.py的样子:

from django.http import HttpResponse 从django.shortcuts导入get_object_or_404,从basicapp.models导入的导入QueryForm def index(request): form = MyForm() real_form = form.getForm(request) response = form.response return render(reques t,'basicapp / index.html',{'form':real_form,'response':response,}) class MyForm: response = ' def getForm(self,request): form = QueryForm(request.POST)如果form.is_valid(): response = form.cleaned_data ['query' ] form.save()返回表单

现在我正在尝试简单的东西,我正在使用表单的查询字段中的值,并尝试将其发送回页面;到目前为止我失败了。 这是index.html:

< form action =method =post> {% csrf_token%} {{form}} < p> {{response}}< / p> < input type =submitvalue =提交/> < / form>

如果我可以这样做,我认为查询的东西不会那么难。表单正常工作数据被保存在数据库中。只有响应字符串从 views.py 无法在 index.html 表单提交后。您可以帮忙吗?

编辑:根据Hoff的回答,尝试追踪 index.html

< form id =myFormaction =method =get> {%csrf_token%} {{form}} < input type =submitvalue =提交/> < / form> < div id =response> < / div> < script language =JavaScript> $(document).ready(function(){ $(#myForm)。submit(function(){//捕获表单的提交事件 $ .ajax({/ /创建一个AJAX调用... 数据:$(this).serialize(),//获取表单数据类型:$(this).attr('GET'),成功:function(response){// on success .. $(#response)。html(response); //更新DIV } }); return false; }); }); < / script>

仍然没有运气:(

解决方案

views.py

def index(request): questions = None 如果request.GET.get('search'): search = request.GET.get('search') questions = Queries.objects.filter name = request.GET.get('name') query = Queries.object.create(query = search,user_id = name)查询。 save() return render(request,'basicapp / index.html',{'questions':questions,})

html

< form method =GET> 问题:< input type =textname =search>< br /> 名称:< input type = textname =name>< br /> < input type =submitvalue =Submit/> < / form>< br /> ;< br /> {%for问题中的问题%} < p> {{question}}< / p> {%endfor%}

I am quite new to django and struggling to do something very simple. I have a ModelForm for the following model:

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

And I am showing the user a simple form that will help in doing the following:

  • user will ask a question
  • The question will be processed(a database query will be generated based on the question)

  • Then the query result should be shown just beneath the form in the same page.

This is how my views.py looks like:

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

For now I am trying simple stuffs,I am taking the value in query field of the form and trying to send it back to the page;so far I am failed. This is index.html:

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

If I could do this,I think the query stuffs wont be that tough.The form is working fine,the datas are getting saved in database. Only the response string from views.py could not be retrieved inside index.html after form submission. Can you please help?

EDIT: Tried following in index.html based on Hoff's answer:

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

Still no luck :(

解决方案

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

更多推荐

django:在同一页面中输入并显示输出

本文发布于:2023-11-24 21:27:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626906.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:页面   在同一   django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!