英文:
Django-Pass data from html to python and wise versa
问题
我理解你的问题。你想在Django网站中从HTML页面获取数据,然后在Python文件中对数据进行预处理,最后在单击按钮后在同一页上显示新信息。问题是在单击按钮后信息未正确显示在同一页上。以下是你提供的代码的一些问题和建议:
-
urls.py:
- 你的
urls.py
文件中定义了两个路径,但它们都有相同的URL,这可能会导致混淆。你可以为不同的视图使用不同的URL。 - 确保在
urls.py
中正确导入views
模块。
- 你的
-
view.py:
- 在
result
视图中,你应该将结果传递给模板以便在HTML中显示。你可以使用render
函数的第三个参数来传递数据到模板。目前,你传递了一个名为namee
的值,但在模板中使用的变量名是result
。 - 你可以将
result
传递给模板,然后在HTML中使用{{ result }}
来显示它。
- 在
-
Index.html:
- 在HTML模板中,确保你使用
{{ result }}
来显示结果,因为你在视图中传递的变量名是result
,而不是namee
。
- 在HTML模板中,确保你使用
根据你的代码和问题,我提供了一些可能导致问题的地方。你可以尝试按照上述建议进行更正,以确保数据正确显示在同一页上。
英文:
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
以下是您要翻译的内容:
- 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 ashomePage
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>
英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论