Comparing the code with the title of the question, it doesn’t seem to make much sense to me that I’m out of context, so I’ll try to answer myself by paying attention exclusively to the title of the question.
To show how the view "communicates" with the template, create, in the view, two variables (data1
and data2
), initially with the value None
for the two and send to the template where I present a form requesting the two dates and then the Czech code if it was ever sent to the form, if yes, the last send is presented.
To make things easy, I put all the code in just two files (main.py
and template1.html
), this is not usual in Django, but I make comments on the code where each party should be by convention (the ones I remembered).
Copy the content of the topic below "main.py" to a file .py
and the topic "template1.html" obligatorily for a file with that name.
main py.
# Configurações do django, aqui configura-se o projeto django, desde conexoes
# com banco de dados até recursos estaticos e funcionalidades de internacionalização
# Novamente, no 'mundo real', essas configuracoes estariam em um arquivo settings.py
from django.conf import settings
import os
ROOT = os.path.dirname(os.path.abspath(__file__))
settings.configure(
DEBUG=True,
SECRET_KEY = '0uarl&=3a$o1*0wk-5s@x6@d*0%r576h0&@f65+09ebtkv3jtd',
ROOT_URLCONF=__name__,
MIDDLEWARE = (
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
),
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [ROOT]
}
]
)
# No mundo real esse codigo estaria em um arquivo views.py
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
data1 = None
data2 = None
if request.method=='POST':
data1 = request.POST['data1']
data2 = request.POST['data2']
# Faça aqui o que vc quiser com data1 e data2, aqui vou apenas enviar de volta ao site
return render(request, 'template1.html', {'data1': data1, 'data2': data2})
# Para conectar a view à estrutura do site é preciso associa-la a uma URL
# No mundo real esse codigo estaria em um arquivo urls.py
from django.urls import include, path
urlpatterns = (
path('', index),
)
if __name__ == "__main__":
import sys
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
template1.html
<form method="POST">
{% csrf_token %}
<label for="data1">Data Inicial:</label>
<input id="data1" type="date" name="data1" value="" /><br>
<label for="data2">Data Final:</label>
<input id="data2" type="date" name="data2" value="" /><br>
<input type="submit">
{% if data1 != None %}
<br><br>
<p>
Sua última digitação:<br>
Data Inicial: {{data1}}<br>
Data Final: {{data2}}<br>
</p>
{% endif %}
</form>
rotate the .py
from the command line, assuming you have named the main.py
, turn it like this:
python main.py runserver
Point your Rowse to http://127.0.0.1:8000/ and you will see the site requesting two dates, initial and final, from the moment you enter, the site will always inform the last entry.
Exit:
See my answer, just adapt.
– Sidon