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.


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'


def settings(request):
    error = ''
    if request.method == 'POST':
        new_form = TrafficSourcesForm(request.POST, instance=request.POST.get('id'))
        if new_form.is_valid():

            error = request.POST.text

            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})


<table class="table table-striped table-hover">

                <th style="width: 42%">Name</th>
                <th style="width: 43%">Token</th>
                <th style="width: 15%">Action</th>
        {% for form in forms %}

                <td>{{ }}</td>
                <td>{{ form.token }}</td>
                <td><button class="btn btn-lg btn-success w-100" form="{{}}">Save</button></td>


        {% endfor %}
                <td colspan="3">Add new traffic source:</td>
                <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>


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?

Back to Top