How to create a form Django (names and elements) from a column of a DataFrame or QuerySet column.?
How can I create a form - even if it's not a Django form, but a regular one via input? So that the names and elements of the form fields are taken from a column of a DataFrame or QuerySet column. I would like the form fields not to be set initially, but to be the result of data processing - in the form of data from a column of a DataFrame or QuerySet column. The name of the form fields to fill in from the data in Views.
Maybe send a list or a single DataFrame or QuerySet to the template via JINJA?
from django import forms
# creating a form
class InputForm(forms.Form):
first_name = forms.CharField(max_length = 200)
last_name = forms.CharField(max_length = 200)
roll_number = forms.IntegerField(
help_text = "Enter 6 digit roll number"
)
password = forms.CharField(widget = forms.PasswordInput())
from django.shortcuts import render
from .forms import InputForm
# Create your views here.
def home_view(request):
context ={}
context['form']= InputForm()
return render(request, "home.html", context)
<form action = "" method = "post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit">
</form>
How to create a form Django (names and elements of the form fields) from a column of a DataFrame or QuerySet column.?
This is how you can generate a form using a list of column names.
# Create your views here.
def home_view(request):
context ={}
context['fields']= ['column1', 'column2', ... ]
return render(request, "home.html", context)
# home.html
<form>
{% csrf_token %}
{% for name in fields %}
<label for="{{ name }}">{{ name }}</label>
<input type="text" name="{{ name }}">
{% endfor %}
</form>
And to get the column names from a QuerySet or DataFrame, you can do something like this
# from queryset
context['fields'] = list(your_model.objects.all().values()[0])
# or from dataframe
context['fields'] = list(your_dataframe.columns.values)