Django-Pass data from html to python and wise versa

Question:

Index.html

<html>
<head>
<title>my</title>
</head>
<body>
<form class="grid" method="POST" action="{% url 'result' %}">
   {% csrf_token %}
   <textarea name="name" /></textarea>
   <button type="submit"">Detect</button>
</form>
<label>{{ result }}</label>
<body>
</html>

view.py

def test_data_preprocessing(test_data1):
    test_data1 = test_data1.lower()
    test_data1 = re.compile(
        r"(http|https|ftp|ssh)://([w_-]+(?:(?:.[w_-]+)+))([w.,@?^=%&:/~+#-]*[w@?^=%&/~+#-])?"
    ).sub("", test_data1)
    test_data1 = re.compile(r"<.*?>").sub("", test_data1)  # remove tags
    test_data1 = TextBlob(test_data1).correct()  # Speeling correction
    test_data1 = "".join(
        filter(lambda x: not x.isdigit(), test_data1)
    )  # remove numbers
    test_data1 = contractions.fix(test_data1)  # contraction
    test_data1 = remove_stop(test_data1)  # stop word removal
    test_data1 = re.compile(r"[^ws]").sub(
        "", test_data1
    )  # '[^ws]',''   this convert all puntuations, underscores, alphabets to null(not of word characters and space)
    test_data1 = word_tokenize(test_data1)  # Tokenization
    test_data1 = [
        lemmatizer.lemmatize(w, get_wordnet_pos(w)) for w in test_data1
    ]  # Lemmatization
    return test_data1
def result(request):
    namee = request.POST['name']
    namee =  test_data_preprocessing(namee)
    return render(
    request,
        "index.html",
        {"name":namee}
    )

urls.py

from django.urls import path
from mywebsite import views
urlpatterns = [
    path("", views.homePage, name='result'),
    path("", views.result),
]

I’m making a website using Django where I have to get data from html page in python file and after getting data I have to perform some preprocessing on data. and after that again return back to the same page with some new information when i press a button. The problem is that when I press button than I move to the same page, but the new information is not displaying after clicking on a button. Is there any way to move information in the same page?

Asked By: code running

||

Answers:

The problem is with action attribute of HTML if you see it correctly, there should be "" in action, currently they are missing so it should be:

action="{% url 'result' %}"

Not:

action={% url 'result' %}

Note: You can also simply remove the action attribute as Django by default takes current page route.

Secondly, your view name is result in views.py and you define it as homePage in urls.py, kindly see it.

Also the view should pass some context dict so:

def result(request):
    name = request.POST['name']
    actual_name=preprocessing(name)
    return render(
    request,
        "index.html",
        {"name":actual_name}
    )

Edit:

Try this urls.py:

from django.urls import path
from mywebsite import views
urlpatterns = [
    path("", views.homePage, name='home-page'),
    path("result/", views.result, name="result"),
]

Try this result view:

def result(request):
    if request.method=="POST":
        namee = request.POST['name']
        namee2 =  test_data_preprocessing(namee)
        return render(
            request,
            "index.html",
           {"name":namee2}
        )
     return render(
            request,
            "index.html"
     )

Try this template:

<form class="grid" method="POST" action="{% url 'result' %}">
   {% csrf_token %}
   <textarea name="name" /></textarea>
   <button type="submit"">Detect</button>
</form>

{% if name %}
    <label>{{ name }}</label>
{% else %}
    <p> name is not coming as it is GET request.</p>
Answered By: Sunderam Dubey
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.