Django-将数据从HTML传递到Python,以及反之。

huangapple go评论67阅读模式
英文:

Django-Pass data from html to python and wise versa

问题

我理解你的问题。你想在Django网站中从HTML页面获取数据,然后在Python文件中对数据进行预处理,最后在单击按钮后在同一页上显示新信息。问题是在单击按钮后信息未正确显示在同一页上。以下是你提供的代码的一些问题和建议:

  1. urls.py:

    • 你的urls.py文件中定义了两个路径,但它们都有相同的URL,这可能会导致混淆。你可以为不同的视图使用不同的URL。
    • 确保在urls.py中正确导入views模块。
  2. view.py:

    • result视图中,你应该将结果传递给模板以便在HTML中显示。你可以使用render函数的第三个参数来传递数据到模板。目前,你传递了一个名为namee的值,但在模板中使用的变量名是result
    • 你可以将result传递给模板,然后在HTML中使用{{ result }}来显示它。
  3. Index.html:

    • 在HTML模板中,确保你使用{{ result }}来显示结果,因为你在视图中传递的变量名是result,而不是namee

根据你的代码和问题,我提供了一些可能导致问题的地方。你可以尝试按照上述建议进行更正,以确保数据正确显示在同一页上。

英文:

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"[^\w\s]").sub(
        "", test_data1
    )  # '[^\w\s]',''   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?

答案1

得分: 1

以下是您要翻译的内容:

  1. 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.

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

  2. 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}
    )
  1. 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"),
]
  1. 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"
     )
  1. 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>
英文:

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>

huangapple
  • 本文由 发表于 2023年2月8日 13:58:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定