ModelForm won't create instance of model

James Kolber

I am trying to create a frontend form in my Django site that will allow users to add entries to my SQL database.

But when I use the form nothing happens in my database. What am I doing wrong?

I thought the right way would be to use the ModelForm technique.

My models looks like this:

class Actor(models.Model):
    name = models.CharField(max_length=200)
    wage = models.IntegerField(default=3100)

    def __str__(self):
        return self.name

So I wrote this in my forms.py:

from django import forms
from .models import Actor

class ActorForm(forms.ModelForm):
     class Meta:
        model = Actor
        fields = ['name', 'wage']
form = ActorForm()

I then added this to my views.py:

def get_actor(request):
    if request.method == 'POST':
        form = ActorForm(request.POST)
        if form.is_valid():
            return HttpResponseRedirect('/scenes/thanks/')
    else:
        form = ActorForm()

    return render(request, 'scenes/actor.html', {'form': form})

def thanks(request):
    return render(request, 'scenes/thanks.html',)

And this in a template called actors.html

<form action="/scenes/actor/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />

Alasdair

You have to call the model form's save() method after checking that it's valid:

def get_actor(request):
    if request.method == 'POST':
        form = ActorForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/scenes/thanks/')
    else:
        form = ActorForm()

    return render(request, 'scenes/actor.html', {'form': form})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

codeigniter won't create a model and won't tell me why not

From Dev

Save a model instance in afterSave(), won't it run recursive?

From Dev

Can't create new instance of django model

From Dev

Test if Django ModelForm has instance on customized model

From Dev

Django: select a instance of a parent model at ModelForm

From Dev

Why won't a DirectoryInfo instance (re)create a folder after deleting it?

From Dev

Django ModelForm - Create instance with foreign key

From Dev

create a model instance with associations

From Dev

create a model instance with associations

From Dev

I can't create model objects using accepts_nested_attributes_for. It won't create the nested object

From Dev

I can't create model objects using accepts_nested_attributes_for. It won't create the nested object

From Dev

Can't load form to create new instance of model

From Dev

Django restrict options of ManyToMany field in ModelForm based on model instance

From Dev

Django restrict options of ManyToMany field in ModelForm based on model instance

From Dev

Django ModelForm won't allow Null value for required field?

From Dev

Angular model won't register

From Dev

Create instance of model with field with choices

From Dev

Create instance of model from method

From Dev

rails 4 nested attributes won't create has_many model

From Dev

How to create generic modelform field validator for inherited model attributes in django?

From Dev

Create new object on ModelForm save, with values from related model

From Dev

How to create generic modelform field validator for inherited model attributes in django?

From Dev

How can I create Django ModelForm for an abstract Model?

From Dev

ModelForm with Instance = Session ID

From Dev

How should I validate the presence of fields that create an instance that doesn't have a model?

From Dev

Rails 4 model is valid, but won't save?

From Dev

Django Model Won't Validate or Not Installed

From Dev

Why won't the data in the model persist?

From Dev

CodeIgniter won't load model on deployment machine

Related Related

  1. 1

    codeigniter won't create a model and won't tell me why not

  2. 2

    Save a model instance in afterSave(), won't it run recursive?

  3. 3

    Can't create new instance of django model

  4. 4

    Test if Django ModelForm has instance on customized model

  5. 5

    Django: select a instance of a parent model at ModelForm

  6. 6

    Why won't a DirectoryInfo instance (re)create a folder after deleting it?

  7. 7

    Django ModelForm - Create instance with foreign key

  8. 8

    create a model instance with associations

  9. 9

    create a model instance with associations

  10. 10

    I can't create model objects using accepts_nested_attributes_for. It won't create the nested object

  11. 11

    I can't create model objects using accepts_nested_attributes_for. It won't create the nested object

  12. 12

    Can't load form to create new instance of model

  13. 13

    Django restrict options of ManyToMany field in ModelForm based on model instance

  14. 14

    Django restrict options of ManyToMany field in ModelForm based on model instance

  15. 15

    Django ModelForm won't allow Null value for required field?

  16. 16

    Angular model won't register

  17. 17

    Create instance of model with field with choices

  18. 18

    Create instance of model from method

  19. 19

    rails 4 nested attributes won't create has_many model

  20. 20

    How to create generic modelform field validator for inherited model attributes in django?

  21. 21

    Create new object on ModelForm save, with values from related model

  22. 22

    How to create generic modelform field validator for inherited model attributes in django?

  23. 23

    How can I create Django ModelForm for an abstract Model?

  24. 24

    ModelForm with Instance = Session ID

  25. 25

    How should I validate the presence of fields that create an instance that doesn't have a model?

  26. 26

    Rails 4 model is valid, but won't save?

  27. 27

    Django Model Won't Validate or Not Installed

  28. 28

    Why won't the data in the model persist?

  29. 29

    CodeIgniter won't load model on deployment machine

HotTag

Archive