首页 > 解决方案 > 如何在Django中提交按钮后清除表单字段

问题描述

我有这个 Views.py

from django.shortcuts import render,redirect
from django.views.generic.edit import FormView
from .forms import ReportForm
from django.views import View
import spacy
import pdb
nlp = spacy.load('en_core_web_sm')


class stopwordsremoval(FormView):
    template_name = 'report.html'
    form_class = ReportForm
    #pdb.set_trace()

    def form_valid(self, form):
        document1 = (form.cleaned_data.get('text'))

        return redirect('report_details', document1)
        return http.HttpResponseRedirect('')

class ReportDetails(View):
    def get(self,request,document1):
        word_lemma = []
        #pdb.set_trace()
        for word in (nlp(document1)):
            a = (word.is_stop, word.text)
            word_lemma.append(a)
        searchedData = (word_lemma)
        return render(request, 'report.html', {'naturallanguageprocessing': searchedData})

表格.py

from django import forms
import pdb

class ReportForm(forms.Form):
     text = forms.CharField(widget=forms.Textarea(attrs={'rows': 5, 'cols': 100}))

它正在创建表单并将其呈现在下面是 html 代码

html

  {% extends 'base.html' %}

 {% block content %}
 <h1>StopWordsRemoval</h1>
          <div>
          <form action = "" method = "post" >
               {% csrf_token %}

                  {{ form.as_p}}


          <button type="submit">stopwordsremoval</button>
               </div>



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

我的问题是,当单击按钮表单将显示时,我们可以在同一位置输入输入并获取输出,但按钮保持相同的表单将不可见。我的要求是一切都应该只有当我单击按钮时,它的输出才会显示在下面,并且表单应该保持不变与输入相同。

标签: pythonhtmldjango

解决方案


推荐阅读