Answer the question
In order to leave comments, you need to log in
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})
{{form.name}}
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 questionAsk a Question
731 491 924 answers to any question