Home > Enterprise >  Model form is not able to save in database
Model form is not able to save in database

Time:10-25

I am a beginner in Django I want to save a form data in database but i am not able to save, followed some tutorials also.

form.py:

from django.forms import ModelForm

from .models import *

class listsForm(ModelForm):

    class Meta:
        model = todo
        fields = "__all__"

views.py:

from django.shortcuts import render

from .models import *

from .form import *

def index(request):

    lists = todo.objects.all()
    form = listsForm()
    context = {
        'lists':lists,
        'form':form,
    }
    if request.method == 'POST':
        form = listsForm(request.POST)
        if form.is_valid:
            form.save()

    return render(request, 'index.html', context)

models.py:

from django.db import models

class todo(models.Model):

    title = models.CharField(max_length=200)
    description = models.TextField(null=True, blank=True)
    created = models.DateField(auto_now_add=True)

    def __str__(self):
        return self.title

CodePudding user response:

Why are you rendering listsForm? Your form should be in the template not rendered!

In index.html, your form should looks like the following:

<form action="{% url 'create_todo' %}" method="POST">
 {% csrf_token %}
 <div class="form-group">
 <label for="title">Title</label>
 <input type="text" name="title" class="form-control" id="title" required></div>
 <div class="form-group">
 <label for="Description">Description</label>
 <textarea name="description" class="form-control" id="description" ></textarea></div>
 <button type="submit" class="btn btn-primary">Submit</button>
 </form>    

In views.py

def index(request):
    return render(request, 'index.html')


def create_todo(request):
    if request.method == 'POST':
       form = listsForm(request.POST)
       if form.is_valid():
       form.save()
       return redirect('index')

In urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('create_todo/', views.create_todo, name='create_todo')
]

You will still need to render existed todos, preferably in another template.

So in views.py

def alltodos(request):
    todos = Todo.objects.all()
    return render(request, 'index.html', {'todos':todos})

In index.html, above form or after it, it doesn't matter, just for clear visibility

<div class="row justify-content-center mt-5">
        <div class="col-md-10">
        {% if todos %}
            <div class="list-group">
                {% for todo in todos %}
                    <a class="list-group-item list-group-item-action><b>{{ todo.title }}</b>{{ todo.description|truncatechars:30 }}{% endif %}</a>
                {% endfor %}
            </div>
        {% else %}
            <div hljs-string">r">
                <h2>Looks like you don't have any todos!</h2>
            <br>
            </div>
        {% endif %}
        </div>
    </div>

In urls.py add

  path('todos', views.alltodos, name='alltodos'),

Advanced project of mine

CodePudding user response:

I have find out why it was not working,

I was using <input type="button"> for submit button but when I changed it to <button type="submit"> it works.

  • Related