Home > front end >  Django ModelForm, not saving data
Django ModelForm, not saving data

Time:12-06

I'm using ModelForm to create a form, then I'm using this form to create a new instance in the database. But the form is not saving data, it's just reloading the page when the form submmited.

I'm also uploading an image.

forms.py

from django import forms
from django_summernote.widgets import SummernoteWidget

from events.models import Event


class EventCreateForm(forms.ModelForm):
    class Meta:
        model = Event
        fields = "__all__"
        widgets = {
            "content": SummernoteWidget(),
        }

    def __init__(self, *args, **kwargs):
        super(EventCreateForm, self).__init__(*args, **kwargs)
        for visible in self.visible_fields():
            visible.field.widget.attrs["class"] = "form-control"

        self.fields["start_datetime"].widget.attrs["class"]  = " datetimepicker"
        self.fields["finish_datetime"].widget.attrs["class"]  = " datetimepicker"
        self.fields["image"].widget.attrs["class"] = "custom-file-input"
        self.fields["address"].widget.attrs["rows"] = 3
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

views.py

from django.urls import reverse_lazy
from django.views.generic import ListView
from django.views.generic.edit import FormView
from django.contrib.auth.mixins import LoginRequiredMixin

from events.models import Event
from events.forms import EventCreateForm


class EventListView(LoginRequiredMixin, ListView):
    model = Event
    context_object_name = "events"


class EventFormView(LoginRequiredMixin, FormView):
    template_name = "events/event_form.html"
    form_class = EventCreateForm
    success_url = reverse_lazy("dashboard:events:home")
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

event_form.html

<form action="" method="post" enctype="multipart/form-data">
            {% csrf_token %}          

            <div class="card-body">
              <div class="form-group">
                <label>Title</label>
                {{ form.title }}
              </div>
              <div class="row">
                <div class="col-sm-4">                  
                  <div class="form-group">
                    <label>Start Date and Time</label>
                      {{ form.start_datetime }}                                
                  </div>
                </div>

                <div class="col-sm-4">
                  <div class="form-group">
                    <label>Finish Date and Time</label>
                      {{ form.finish_datetime }} 
                  </div>
                </div>
                
                <div class="col-sm-4">
                  <div class="form-group">
                    <label for="exampleInputFile">Image</label>
                    <div class="input-group">
                      <div class="custom-file">
                        {{ form.image }}
                        <label class="custom-file-label">Choose file</label>
                      </div>                     
                    </div>
                  </div>
                </div>
              </div>              
              <div class="form-group">
                <label>Address</label>
                {{ form.address }}
              </div>              
              <div class="form-group">
                <label>Content</label>
                {{ form.content }}
              </div>              
            </div>
            <!-- /.card-body -->
            
            <div class="card-footer">
              <button type="submit" class="btn btn-primary">Publish</button>
              <a href="#" class="btn btn-default float-right">Cancel</a>
            </div>
          </form>
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

A FormView does not save the form. The .form_valid(…) method can be overridden to specify what to do in case the form is valid. It makes here more sense to use a CreateView [Django-doc], which will save the form and thus store the data into the database:

from django.views.generic import CreateView

class EventFormView(LoginRequiredMixin, CreateView):
    template_name = "events/event_form.html"
    form_class = EventCreateForm
    model = Event
    success_url = reverse_lazy("dashboard:events:home")
  • Related