我是新手!!我有一项任务是根据从下拉菜单中选择的选项填充字段值。
Forms.py
from django import forms
from .models import MainPage
import datetime
from datetime import date
techlist = (
('DateOfDrive', 'DateOfDrive'),
('NameOfDrive', 'NameOfDrive'),
('Status', 'Status'),
('Marks_Scored', 'Marks_Scored'),
('First_Round_Interviewer_Name', 'First_Round_Interviewer_Name'),
('Second_Round_Interviewer_Name', 'Second_Round_Interviewer_Name'),
('Third_Round_Interviewer_Name', 'Third_Round_Interviewer_Name'),
('Management_Round_Interviewer_Name', 'Management_Round_Interviewer_Name'),
('HR_Round_Interviewer_Name', 'HR_Round_Interviewer_Name')
)
class analysisForm(forms.Form):
GroupBy = forms.ChoiceField(choices=techlist)
where = forms.CharField()
datefrom = forms.DateField(initial="2019-01-01")
dateto = forms.DateField(initial=date.today())
views.py
class analysis(FormView):
template_name = 'analysis.html'
# View for the analysis page
def get(self, request):
form = analysisForm()
return render(request, self.template_name, {'form': form})
def post(self, request):
form = analysisForm(request.POST)
if form.is_valid():
groupbyf = form.cleaned_data['GroupBy'] # Getting the groupby otpion from the user
wheref = form.cleaned_data['where'] # Getting the search element from the user
datetof = form.cleaned_data['dateto'] # Getting the end date from the user
datefromf = form.cleaned_data['datefrom'] # Getting the from date from the user
# Loading the excel to data frame
query = str(Summary.objects.all().query)
df_view_summary = pd.read_sql_query(query, connection)
print(df_view_summary)
# Extracting the month number from the complete date
df_view_summary['Month'] = pd.DatetimeIndex(df_view_summary['DateOfDrive']).month
analysis.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post">
{% csrf_token %}
<table>
{{ form.as_p }}
</table>
<input type="submit">
</form>
# Loading the excel to data frame
query = str(Summary.objects.all().query)
df_view_summary = pd.read_sql_query(query, connection)
print(df_view_summary)
# Extracting the month number from the complete date
df_view_summary['Month'] = pd.DatetimeIndex(df_view_summary['DateOfDrive']).month
</body>
</html>
我现在应该根据从表单中下拉groupby字段中选择的值,在django中填充where字段,我该怎么做。在下拉列表中选择一个值后,立即在where字段中填充将由视图发送的值。 如何在没有Ajax / Javascript的情况下执行此操作。