简体   繁体   English

Django:无需重新加载即可更新页面

[英]Django: update a page without reloading

I want to update my homepage.html with button action and show some result from the server.我想用按钮操作更新我的homepage.html并显示来自服务器的一些结果。 But the problem is when I click on the button the whole page is reloading.但问题是当我点击按钮时,整个页面都在重新加载。 Here, my project name is "T2KG".在这里,我的项目名称是“T2KG”。 My form tag looks like this:我的表单标签如下所示:

<form method="POST">
        {% csrf_token %}

        <center><textarea placeholder="Give a input sentence....."
      class="text" name="sent" rows="5" cols="70" font="15px arial, sans-serif" autofocus>Barack Obama born in Hawaii. Hawaii locates in USA.</textarea></center><br>
        <button type="submit" class="btn btn-primary btn-block" id="display_result">Generate Triple</button>
</form>

By searching in many websites I have understood that my view.py and urls.py is not correct and also I have to use AJAX.通过在许多网站上搜索,我了解到我的 view.py 和 urls.py 不正确,而且我必须使用 AJAX。 But How to implement in this situation I don't know.但是我不知道在这种情况下如何实施。 I have tried but couldn't find any way out.我试过了,但找不到任何出路。 In view.py I return the value like this:在 view.py 中,我返回这样的值:

def result(request):
text = 'null'
if request.method == 'POST':
    form_input = Sentence(request.POST)
    if form_input.is_valid():
        text = form_input.cleaned_data['sent']
    else:
        form_input = Sentence()

triples = getTriples(text)

ent_list = entityList(text)
triples = predicateList(aggregate(triples, ent_list))
return render(request, './T2KG/homepage.html',{'triples': triples, 'json_triples': json.dumps(triples), 'text':text})

And my urls.py is:我的 urls.py 是:

urlpatterns = [
path('', views.set_sent, name='set_text'),
url(r'^T2KG/homepage', views.result, name='result'),]+static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Please help me.请帮我。

What your looking is an ajax call to update the data on the page without the whole page refresh.你看到的是一个 ajax 调用来更新页面上的数据而不刷新整个页面。 For that, you would require a javascript call to send the form data to the backend and receive a json object back on success.为此,您需要调用 javascript 将表单数据发送到后端,并在成功后接收 json 对象。

You can write a separate view for that or handle that by method check in the same view at python end(I prefer sep view for APIs).您可以为此编写一个单独的视图,也可以在 python 端的同一视图中通过方法检查处理该视图(我更喜欢 API 的 sep 视图)。 If using the same view remember to return the JSON object instead of a template renderer.如果使用相同的视图,请记住返回 JSON 对象而不是模板渲染器。

What javascript is doing is simply taking form data from your webpage and sending it to the designated URL then waiting for the response. javascript 所做的只是从您的网页中获取表单数据并将其发送到指定的 URL,然后等待响应。 For it to apply changes it needs a response(#can read about js promises).为了应用更改,它需要一个响应(#can read about js promises)。 So there need to be a backend(python in this case) to listens to this call and return appropriate data(on success).所以需要有一个后端(在这种情况下是 python)来监听这个调用并返回适当的数据(成功时)。

A nice follow links can be:一个不错的关注链接可以是:

https://www.geeksforgeeks.org/handling-ajax-request-in-django/ https://www.geeksforgeeks.org/handling-ajax-request-in-django/

https://simpleisbetterthancomplex.com/tutorial/2016/08/29/how-to-work-with-ajax-request-with-django.html https://simpleisbetterthancomplex.com/tutorial/2016/08/29/how-to-work-with-ajax-request-with-django.html

http://www.tangowithdjango.com/book17/chapters/ajax.html http://www.tangowithdjango.com/book17/chapters/ajax.html

Ajax 就是为了解决这个问题而创建的,在第一次刷新页面后,服务器的所有其他更新和数据都由 ajax 获取,如果你想开发一个动态网页,你需要了解 Ajax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM