Как заставить django взаимодействовать с html
So, I have the following question, I need to create a way for the object inserted within the field to be transferred to a
within a single page but with different functions, and I managed to do this but in a way that In my opinion, it shouldn't be the best, that is, my goal is to optimize this if possible. Below is the code for the views, urls, forms, html.
And detail, I don't intend to use Java, but if this is the only or best way, I'm willing to understand.
#urls
from django.urls import path
from FrontEndProject import views
urlpatterns = [
path('', views.RenderHomePage),
path('test/', views.PostInfoHomePage, name='PostInfoHomePage')
]
#views
from django.shortcuts import render
from .forms import *
def RenderHomePage(request):
return render(request, 'HomePageBody.html')
def PostInfoHomePage(request):
Info = ''
if request.method == 'POST':
form = FormInfo(request.POST)
if form.is_valid():
Info = form.cleaned_data['PostInfo']
else:
form = FormInfo()
return render(request, 'HomePageBody.html', {'form': form, 'Info': Info})
#forms
from django import forms
class FormInfo(forms.Form):
PostInfo = forms.CharField()
#html
<!DOCTYPE html>
<html>
<head>
<title>
Home page
</title>
</head>
<body>
<div>
<form method="post" action="{% url 'PostInfoHomePage' %}">
{% csrf_token %}
<input name="PostInfo" value="{{ form.PostInfo.value|default:'' }}">
<button>Take info</button>
</form>
<p>Information: {{ Info }} </p>
</div>
</body>
</html>