Can we add a HTML attr with value from our DB in Django?
I have a form, I take the info to this form from my DB.
My form.py
class TrafficSourcesForm(ModelForm):
class Meta:
model = TrafficSources
fields = ['name', 'token']
widgets = {
'name': TextInput(attrs={
'class': 'form-control',
'placeholder': 'Traffic source name'
}),
'token': TextInput(attrs={
'class': 'form-control',
'placeholder': 'API-token'
})
}
My views.py
def settings(request):
error = ''
if request.method == 'POST':
new_form = TrafficSourcesForm(request.POST, instance=request.POST.get('id'))
if new_form.is_valid():
# new_form.save()
error = request.POST.text
else:
error = 'Something went wrong!'
new_form = TrafficSourcesForm()
forms = [TrafficSourcesForm(instance=x) for x in TrafficSources.objects.all()]
return render(request, 'mainpage/dashboard.html', {'new_form': new_form, 'forms': forms, 'error': error})
MY HTML
<table class="table table-striped table-hover">
<tr>
<th style="width: 42%">Name</th>
<th style="width: 43%">Token</th>
<th style="width: 15%">Action</th>
</tr>
{% for form in forms %}
<tr>
<td>{{ form.name }}</td>
<td>{{ form.token }}</td>
<td><button class="btn btn-lg btn-success w-100" form="{{form.instance.id}}">Save</button></td>
</tr>
{% endfor %}
<tr>
<td colspan="3">Add new traffic source:</td>
</tr>
<tr>
<td><input class="form-control" placeholder="Name"></td>
<td><input class="form-control" placeholder="API-token"></td>
<td><button class="btn btn-lg btn-success w-100">Add</button></td>
</tr>
</table>
As I am using a table grid, my forms for the inputs and submit button are outside the table. In that way I need to put a tag form="{a variable from DB to match the form}" on my inputs. Something like instance number or name field from DB would be nice. And as I am making my inputs with {form.input} I have no Idea how to add this tag there with the information from my data base. Can you help?