Home > Blockchain >  Django does not save the form (nothing happens)
Django does not save the form (nothing happens)

Time:01-31

\\models.py

class BasicSettings(models.Model):
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        default=None,
        null=True,
        on_delete=models.CASCADE,
    )
    settings_description = models.BooleanField(default=False)
    settings_photo = models.BooleanField(default=False)
    settings_username = models.BooleanField(default=False)
    settings_background = models.BooleanField(default=False)

\\views.py

    try:
        basic_settings = request.user.basicsettings
    except BasicSettings.DoesNotExist:
        basic_settings = BasicSettings(user=request.user)

    if request.method == 'POST':
        basicsettings_form = BasicSettingsForm(
            request.POST, instance=basic_settings)

    else:
        basicsettings_form = BasicSettingsForm(instance=basic_settings)


return render(request, 'dashboard.html', {'basicsettings_form': basicsettings_form})

\\forms.py

class BasicSettingsForm(forms.ModelForm):
    class Meta:
        model = BasicSettings

        fields = (
            'settings_description',
            'settings_photo',
            'settings_username',
            'settings_background',
        )

        widgets = {
            'settings_description': forms.TextInput(attrs={'type': 'checkbox', 'class': 'checkbox', 'id': 'test1'}),
            'settings_photo': forms.TextInput(attrs={'type': 'checkbox', 'class': 'checkbox', 'id': 'test2'}),
            'settings_username': forms.TextInput(attrs={'type': 'checkbox', 'class': 'checkbox', 'id': 'test3'}),
            'settings_background': forms.TextInput(attrs={'type': 'checkbox', 'class': 'checkbox', 'id': 'test4'}),
        }

\.html

<form method="POST">
  {% csrf_token %}
{{ basicsettings_form }}
<button type="submit">Dad9adbaiduaadtest</button>

When I click on the submit button, the page just refreshes but nothing happens. What might be the reason for this?

The code above is just a snippet. Maybe nothing happens because I have multiple forms in one view? The whole view looks like this https://pastebin.com/Bu5NLTX1 but I just wanted to give you the relevant part

CodePudding user response:

This might be the problem..

From Creating forms from models:

Every ModelForm also has a save() method. This method creates and saves a database object from the data bound to the form. A subclass of ModelForm can accept an existing model instance as the keyword argument instance; if this is supplied, save() will update that instance. If it’s not supplied, save() will create a new instance of the specified model:

So maybe try:

basicsettings_form = BasicSettingsForm(
            request.POST, instance=basic_settings)
if basicsettings_form.is_valid():
    basicsettings_form.save()

CodePudding user response:

Have you added action in your form action=your_action_url

<form action ="{% url 'your_url_name' %}" method="POST">
  {% csrf_token %}
{{ basicsettings_form }}
<button type="submit">Dad9adbaiduaadtest</button>
  •  Tags:  
  • Related