H
H
howuu2019-06-07 17:15:01
Django
howuu, 2019-06-07 17:15:01

Instance not working in form?

The form saves the data, but does not display it as an instance, that is, if we want to edit something, the form is empty, and it is expected that there will be a placeholder, I don’t understand why it doesn’t work on another page, almost identical code

from .models import Config
from .forms import  ConfigForm
from django.contrib.auth.decorators import login_required

@login_required
def settings(request):
    config = Config.objects.all().first()
    form = ConfigForm(request.POST, instance=config)
    if request.method == "POST":
        if form.is_valid():
            config_set = form.save()
        return redirect('/settings')
    else:
        return render(request, 'settings.html', {'config': config, 'form': form})

This is how I display the form field in html (of course, everything is wrapped with the rest of the form, it works and the object is displayed in the admin panel, the instance itself is not displayed when I want to edit the object created in this form)
{{form.name}}
here is the form code
from django import forms
from .models import Config

class ConfigForm(forms.ModelForm):
    class Meta:
    	
        model = Config
        fields = ('name')

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question