Skip to content Skip to sidebar Skip to footer

Pre-populate Html Form Table From Database Using Django

I have a class-based view (IndexView at views.py) that shows a table with all the data stored in the database. This view is rendered in index.html using a def get_queryset(self) to

Solution 1:

what about using generic from django.views.generic.edit import UpdateView straight on model?

https://docs.djangoproject.com/en/2.0/ref/class-based-views/generic-editing/#updateview

views.py:

from django.forms import ModelForm, CharField, Textarea
from django.urls import reverse_lazy
from django.views.generic import UpdateView

from demo.models import Item


classItemForm(ModelForm):
    description = CharField(widget=Textarea)

    classMeta:
        model = Item
        fields = ['code', 'amount']

    defsave(self, commit=True):
        item = super(ItemForm, self).save(commit=commit)
        item.code.description = self.cleaned_data['description']
        item.code.save()

    defget_initial_for_field(self, field, field_name):
        if field_name == 'description':
            return self.instance.code.description
        else:
            returnsuper(ItemForm, self).get_initial_for_field(field, field_name)


classItemUpdateView(UpdateView):

    form_class = ItemForm
    model = Item

    defget_success_url(self):
        return reverse_lazy('item-detail', kwargs={'pk': 1})

urls.py

from django.conf.urls import url
from django.contrib import admin
from django.urls import path


from demo.views import ItemUpdateView

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    path(r'items/<int:pk>/', ItemUpdateView.as_view(), name='item-detail')
]

/templates/demo/item_form.html

<formaction=""method="post">{% csrf_token %}
    {{ form.as_p }}
    <inputtype="submit"value="Update" /></form>

If something regarding settings or config is unclear please refer to the repo with demo: https://github.com/andilabs/cbv

UPDATE

added modelformset producing what you need in ListView

from django.forms import modelformset_factory


classItemListView(ListView):
    model = Item

    def get_context_data(self, **kwargs):
        data = super(ItemListView, self).get_context_data()
        formset = modelformset_factory(Item, form=ItemForm)()
        data['formset'] = formset
        returndata

this just displays data in forms, you have take care of rest.

Solution 2:

There are multiple ways of loading data into forms, as far as I know:

  • In views.py

You are using FormViewCBV which has a func called get_intial:

defget_initial(self):
        initial = super().get_initial()
        initial['<form-field>'] = <initial-value>
        return initial
  • In forms.py

You can override the init func:

def__init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['<form-field>'].initial = <initial-value>

Solution 3:

One thing you can do is iterate through the item list, and set the value attribute of the input tag of each field to form.field_name.value

Post a Comment for "Pre-populate Html Form Table From Database Using Django"