How to refresh a dropdown as soon as a new value is added to the linked FK field in a form?
View ‘EntryView’ has got a dropdown field.
there is button next to that field to enable the user adding new values to that dropdown.
<button type="button" class="btn btn-secondary mb-3" hx-get="{% url 'entry:add-new' %}" hx-target="#dialog" class="btn btn-dark">
<i class="fa fa-plus"></i>
</button>
By clicking that button a modal form will be opened to get the inputs from user. On clicking ‘Save’ of that modal form, value is getting saved to the corresponding table. Only upon the page refresh can the user see the newly added value. (Using Django Htmx modal)
We would like that dropdown/form to be refreshed as soon as the ‘Save’ button is clicked.
Even if there would be a solution to make that newly added value appear on the dropdown as soon as the ‘Save’ button on modal form is clicked, that would be very useful.
Request help for the same. - Thank you!
urlpatterns:
app_name = 'entry'
urlpatterns = [
path('',IndexView.as_view(),name='index'),
path('entry/',views.EntryView,name='entryies'),
path('addnew/',views.AddNewView,name='add-new'),
]
views.py
@login_required
def AddView(request):
addForm = AddForm(request.POST or None)
if request.method == "POST":
if addForm.is_valid():
addForm.save()
return HttpResponseRedirect(reverse('entry:entries'))
else:
addForm = AddForm()
context = {
'addForm': addForm
}
return render(request,"entry/addNew.html",context)
@login_required
def EntryView(request):
entryFields = Entry.objects.all()
entryForm = EntryForm(request.POST or None)
if request.method == "POST":
if entryForm.is_valid():
entryForm.save()
return HttpResponseRedirect(reverse('entry:entries'))
else:
entryForm = EntryForm()
return render(request,"entry/entry.html",{
'entryForm':entryForm})
context = {
'entryForm': entryForm,
'entryies': entryFields
}
return render(request,"entry/entry.html",context)