简体   繁体   English

结合使用Ajax和Django来调用后端并显示结果

[英]Using Ajax with Django to call backend and display result

I am taking user input and sending it to Django backend for some text processing. 我正在接受用户输入,并将其发送到Django后端进行一些文本处理。 After performing backend operation I want to display results. 执行后端操作后,我想显示结果。

I tried but as I am new with ajax,I am not sure where I am making mistake. 我尝试过,但是由于我对ajax不熟悉,所以我不确定在哪里出错。 Can anyone please give me correct way of using ajax for this operation? 任何人都可以给我正确的方法来使用ajax进行此操作吗? I appreciate if you can give link to any good reference document. 如果您可以提供任何参考文件的链接,我们将不胜感激。

Current issue - When I click on submut button it removes input text area. 当前问题-当我单击submut按钮时,它将删除输入文本区域。

在此处输入图片说明 在此处输入图片说明

My html report.html - 我的html report.html-

{% extends 'base.html' %}
{% block content %}
  <h1>StopWordsRemoval</h1>

<script type='text/javascript'>
$(document).ready(function(){
    $(form).on('submit', function(event){
        $.ajax({
            url: 'stopwordsremoval/(?P<document1>.+)'
            type: 'POST',
            data: this.serialize(),
        }); 
    });
});
</script>

<div>
<form  method = "post" >
    {% csrf_token %}
    {{ form}}

    <button type="submit">stopwordsremoval</button>
    </div>
    </form>
    <div >
        <ul>
            <li>{{ naturallanguageprocessing }}</li> 
        </ul>
    <a href="{% url 'stopwordsremoval' %}" </a>
<style>
    div {
        width: 1000px;
        border: 5px solid green;
        padding: 10px;
        margin: 10px;
    }
</style>
</div>
  </div>

  {% endblock %}

urls.py urls.py

from django.conf.urls import url    
from . import views
urlpatterns = [
    url(r'stopwordsremoval/$', views.stopwordsremoval.as_view(), name='stopwordsremoval'),
    url(r'stopwordsremoval/(?P<document1>.+)/$',
        views.ReportDetails.as_view(), name='report_details'),
]

forms.py 表格

from django import forms

class ReportForm(forms.Form):
    #pdb.set_trace()
    text = forms.CharField(widget=forms.Textarea(attrs={'rows': 5, 'cols': 100}))
<script type='text/javascript'>
$(document).ready(function(){
    $(button).on('click', function(event){
        var form = $(form).serialize()
        $.ajax({
            url: 'stopwordsremoval/(?P<document1>.+)' // this will not work
            type: 'POST',
            data: form,
        }); 
    });
});
</script>

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

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