Skip to content

Instantly share code, notes, and snippets.

@awg01
Last active April 22, 2022 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awg01/33a1443338a11effc3218fc1b5577c6b to your computer and use it in GitHub Desktop.
Save awg01/33a1443338a11effc3218fc1b5577c6b to your computer and use it in GitHub Desktop.
code for rendering two forms in one page

views.py logic

def paragraph(request):
    form1 = PSummaryForm1()
    form2 = PSummaryForm2()
    # return render(request, 'paragraph.html', {'form1': form1, 'form2': form2 })
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        form1 = PSummaryForm1(request.POST)
        if request.POST.get("form_type") == 'formOne':
            # create a form instance and populate it with data from the request:
            form1 = PSummaryForm1(request.POST)
            print("paragraph1")
            print("form1 content")
            print(form1)
            if form1.is_valid():
                # process the data in form.cleaned_data as required
                paragraph = form1.cleaned_data['paragraph'].strip()
                length = form1.cleaned_data['length']
                if length < 0:
                    length = 10
                # paragraph_summary1 = retrieve_input(paragraph, length)
                paragraph_summary1 = "this is summary paragraph1"
                return render(request, "paragraph.html", {'paragraph_summary1':paragraph_summary1, 'form1': form1, 'form2': form2})
                # return render(request, 'paragraph.html')
        if request.POST.get("form_type") == 'formTwo':
            # create a form instance and populate it with data from the request:
            form2 = PSummaryForm2(request.POST)
            print("paragraph2")
            print("form2 contents")
            print(form2)
            if form2.is_valid():
                # process the data in form.cleaned_data as required
                paragraph = form2.cleaned_data['paragraph'].strip()
                length = form2.cleaned_data['length']
                if length < 0:
                    length = 10
                # paragraph_summary2 = retrieve_input(paragraph, length)
                paragraph_summary2 = "this is summary paragraph2"
                return render(request, "paragraph.html", {'paragraph_summary2':paragraph_summary2, 'form1': form1, 'form2': form2})
    else:
        print("inside main else")
        form = PSummaryForm1()

    return render(request, 'paragraph.html', {'form1': form1, 'form2': form2 })

html template for code rendering

{% extends 'users/base.html' %}
{% load crispy_forms_tags %}

{% block content %}
<form class="container" style="padding-top:10px;" action="" method="POST">
  {% csrf_token %}
  <div class="row summary-row">

    <div class="col-sm summary-col"><!-- col start -->
      <form action="" method="POST" id="formOne">
        {% csrf_token %}
        <input type="hidden" name="form_type" value="formOne">
        <fieldset class="form-group">
            <legend class="border-bottom mb-4" style="color: white;">Form1</legend>
            {{ form1|crispy }}
        </fieldset>
        <div class="form-group" id="signup-button">
            <button class="btn ">Generate summary</button>
        </div>
        <div class="form-group">
                  <textarea class="form-control" id="display_summary" name="display_summary" placeholder="summary of paragraph" value="" rows="15">{{paragraph_summary1}}</textarea>
        </div>
      </form>
    </div><!-- col end -->

    <div class="col-sm summary-col"><!-- col start -->
      <form action="" method="POST" id="formTwo">
        {% csrf_token %}
        <input type="hidden" name="form_type" value="formTwo">
        <fieldset class="form-group">
            <legend class="border-bottom mb-4" style="color: white;">Form2</legend>
            {{ form2|crispy }}
        </fieldset>
        <div class="form-group" id="signup-button">
            <button class="btn ">Generate summary</button>
        </div>
        <div class="form-group">
                  <textarea class="form-control" id="display_summary" name="display_summary" placeholder="summary of url" value="" rows="15">{{paragraph_summary2}}</textarea>
        </div>
      </form>
    </div><!-- col end -->

  </div>

</form>
{% endblock %}

forms used (forms.py)

from django import forms
class PSummaryForm1(forms.Form):
    paragraph = forms.CharField(label='Paragraph to summarize', max_length=10000)
    length = forms.IntegerField(label='Number of sentences', required=False)
    # summary = forms.CharField(label='summary',  max_length=10000, required=False)
class PSummaryForm2(forms.Form):
    paragraph = forms.CharField(label='Paragraph to summarize', max_length=10000)
    length = forms.IntegerField(label='Number of sentences', required=False)
    # summary = forms.CharField(label='summary',  max_length=10000, required=False)

urls.py

from django.urls import path
from . import views
app_name = 'nlp'
urlpatterns = [
    path("paragraph/", views.paragraph, name="paragraph"),
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment