Skip to content Skip to sidebar Skip to footer

Django Python Post Method Not Writing To Database

I now have an issue with my POST Method not writing to the database and not showing in Admin site. the views.py file works until it gets to the 'if form.is_valid()' but doesn't go

Solution 1:

The form is not valid. With sub_type and state, you are passing a choicefield and the model needs a charfield

Your form must be:

classSubscriptionForm(forms.ModelForm):
    firstName = forms.CharField(
        required=True,
        label="First Name",
        widget=forms.TextInput( attrs = {
                'type':"text",
                'placeholder':"First name",
                'class':'form-control', # html input class
        })    
    )

    sub_type = forms.ChoiceField(
        required=True,
        label="Subscription Type",
        choices= (
            ('Option 1', 'Choose'),
            ('Option 2', 'Weekly $ Free'),
            ...
        ),
        widget=forms.Select( attrs = {
            'class':'your-css-class'
        })
    )

    ...

pass the form to your html

defweekly_get(request):
    form = SubscriptionForm()
    return render('weekly.html',{'form':form })

then, your html

<formmethod="post"action="/weekly/"class="needs-validation">
{% csrf_token %}
    
{% for field in form %}
    <divclass="row"><divclass="col-md-6 mb-3">
            {{field.label_tag}}
            {{field}}
        </div></div>
{% endfor %}

</form>

Post a Comment for "Django Python Post Method Not Writing To Database"