0

Im trying to learn Django. I believe my form is working properly but the data is not saved into the database.

This is my model:

from django.db import models

# Create your models here.
class parameters(models.Model):
    interval_1 = models.PositiveIntegerField()
    interval_2 = models.PositiveIntegerField()
    interval_3 = models.PositiveIntegerField()
    interval_4 = models.PositiveIntegerField()
    interval_5 = models.PositiveIntegerField()
    interval_6 = models.PositiveIntegerField()
    cost_1 = models.DecimalField(max_digits=19, decimal_places=2)
    cost_2 = models.DecimalField(max_digits=19, decimal_places=2)
    cost_3 = models.DecimalField(max_digits=19, decimal_places=2)
    cost_4 = models.DecimalField(max_digits=19, decimal_places=2)
    cost_5 = models.DecimalField(max_digits=19, decimal_places=2)
    cost_6 = models.DecimalField(max_digits=19, decimal_places=2)

This is my form:

from django import forms
from django.forms import ModelForm

from .models import parameters

# Create your forms here.

class set_parameters(forms.ModelForm):
    class Meta:
        model = parameters
        fields = "__all__"

This is my view:

from django.shortcuts import render

# Create your views here.
from .models import parameters
from .forms import set_parameters

def frequence_create_view(request):
    form = set_parameters(request.POST or None)
    if form.is_valid():
        form.save()
        print(request)
        print(form)
        form = set_parameters()
    else:
            print (form.errors)

    context = {
        'form': form
    }
    return render(request, 'settings/settings.html', context)

These are my templates:

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
    {% block frequence %}
    {% endblock %}
    {% block cost %}
    {% endblock %}
</body>
</html>

{% extends 'base.html' %}
{% block frequence %}
<form action='.' method='POST'> {% csrf_token %}
    {{ form.as_p }}
    <input type='submit' value='save' />
</form>
{% endblock %}

And this is my URL's

from django.contrib import admin
from django.urls import path
from settings.views import frequence_create_view

urlpatterns = [
    path('admin/', admin.site.urls),
    path('frequence_create/', frequence_create_view),
]

The problem I have is that when i submit the form the information is not stored in the database.

Can somebody try to explain what the problem is?

1
  • have you seen any form errors? Commented Mar 28, 2020 at 8:15

2 Answers 2

1

correct your code as shown, which will handle the get request too

def frequence_create_view(request):
    if request.method == "POST":
        form = set_parameters(request.POST or None)
        if form.is_valid():
           form.save()
           return redirect('/')
    else:
        form = set_parameters()
    return render(request, 'your.html', {'form': form})
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for your responses. I realize my code actually did work, i saw my entries in the database when using the sqlite command line tool. I do however not see my entires in the admin GUI. Thank you bmons for your code suggestion which also works .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.