Using ModelForms

60 views
Skip to first unread message

Hugo Kitano

unread,
Aug 20, 2015, 4:41:05 PM8/20/15
to Django users
Hi, I'm a beginner using Django, and I'm having trouble using model forms to create instances of my model.
When a user submits a form, the database saves nothing.  Here's my code for the view

def submit(request):

SubmissionForm = modelform_factory(Submission, fields=('sub_name', 'sub_file'))

if request.method == "POST":

form = SubmissionForm(request.POST)

if form.is_valid():

model_instance = form.save(commit=False)

model_instance.sub_date = timezone.now()

model_instance.save()

return HttpResponseRedirect('http://localhost:8000/stats/')

else:

form = SubmissionForm()

return render(request, 'stats/submit.html', {'form': form})


And here's my template:


<h1> Submit form: </h1>
<form action="/stats/" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>


I'd appreciate any help. Thansk!


Hugo


Edgar Gabaldi

unread,
Aug 20, 2015, 4:43:32 PM8/20/15
to django...@googlegroups.com
The code seems right. Probably your form has some error. Try print {{form.errors}} in your template to check.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/af4016f0-65e5-4a5c-99e9-7f544e7ced7c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hugo Kitano

unread,
Aug 20, 2015, 5:14:32 PM8/20/15
to Django users
Could you elaborate on my form has some error?

Hugo Osvaldo Barrera

unread,
Aug 21, 2015, 5:25:17 AM8/21/15
to django...@googlegroups.com
 
 
 
On Thu, Aug 20, 2015, at 17:43, Edgar Gabaldi wrote:
The code seems right. Probably your form has some error. Try print {{form.errors}} in your template to check.
 
Shouldn't {{ form.as_p }} include errors as well? This seems to be what the doc indicates:
 
 
For more options, visit https://groups.google.com/d/optout.
 
--
Hugo Osvaldo Barrera
 

Hugo Kitano

unread,
Aug 21, 2015, 1:15:04 PM8/21/15
to Django users, hu...@barrera.io
Do you have any idea what could be causing the problem, then?

James Schneider

unread,
Aug 22, 2015, 6:10:41 AM8/22/15
to django...@googlegroups.com, hu...@barrera.io

>>>> Hi, I'm a beginner using Django, and I'm having trouble using model forms to create instances of my model.
>>>> When a user submits a form, the database saves nothing.  Here's my code for the view
>>>>  

So what exactly does happen? Based on the code you provided, you would either end up on the same page with a blank form (in the event of errors), or you would be redirected back to another page.

Can you validate that your instance.save() call it's actually firing? Have you overridden the save() method in either your form or your model? Try debugging or sprinkling in some print() functions and see whether or not you can trace your way through your code to figure out what is working and what isn't.

-James

James Schneider

unread,
Aug 22, 2015, 6:19:07 AM8/22/15
to django...@googlegroups.com, hu...@barrera.io

On Aug 22, 2015 3:10 AM, "James Schneider" <jrschn...@gmail.com> wrote:
>
> >>>> Hi, I'm a beginner using Django, and I'm having trouble using model forms to create instances of my model.
> >>>> When a user submits a form, the database saves nothing.  Here's my code for the view
> >>>>  
>
> So what exactly does happen? Based on the code you provided, you would either end up on the same page with a blank form (in the event of errors), or you would be redirected back to another page.
>

Sorry, it's late, it probably redirects to the same page, assuming that this view serves the /stats/ URL. Also, the spacing for your code blocks didn't come through so it's hard to tell whether or not you are handling things correctly, or at least on my phone it doesn't.

-James

Hugo Kitano

unread,
Aug 24, 2015, 2:34:41 PM8/24/15
to Django users, hu...@barrera.io
It always redirects to the stats page, though the model instance isn't saved.
I suspect it has to do with the .save() function, but I can't tell why, since I even create the modelform in the views.py file.




Thanks,

Hugo
Auto Generated Inline Image 1
Message has been deleted
Message has been deleted

Hugo Kitano

unread,
Aug 24, 2015, 4:37:04 PM8/24/15
to Django users, hu...@barrera.io
Also, it looks like the request.method is always 'GET', not 'POST', which means the form is never saved.  How is this happening?

On Monday, August 24, 2015 at 12:56:39 PM UTC-7, Hugo Kitano wrote:
More specifically, what does the action="/stats/" field do and require?

On Monday, August 24, 2015 at 12:41:16 PM UTC-7, Hugo Kitano wrote:
I'm actually now pretty certain that my html file is what's causing it to not work

Hugo Osvaldo Barrera

unread,
Aug 24, 2015, 6:29:40 PM8/24/15
to Hugo Kitano, Django users
 
On Mon, Aug 24, 2015, at 16:41, Hugo Kitano wrote:
I'm actually now pretty certain that my html file is what's causing it to not work
 

 
Including your message as an inline image rather than simple text will reduce the amount of people that can actually read your message (and complicate following the thread).
 
Does "/stats/" point to the above mentioned view ("submit")? What does your urls.py look like? Did you try putting a "print" (or anything alike) into the view function to really make sure it's the one being called?
 
--
Hugo Osvaldo Barrera
 
Auto Generated Inline Image 1

Sait Maraşlıoğlu

unread,
Aug 24, 2015, 7:25:49 PM8/24/15
to Django users
instead of
model_instance.sub_date = timezone.now()

I suggest you to declare a default for that field in your model definition.
default = timezone.now()

Hugo Kitano

unread,
Aug 25, 2015, 1:16:32 PM8/25/15
to Django users, hkit...@gmail.com, hu...@barrera.io
Noted.  "/stat/s" is an index page.  Here is what my urls.py looks like.  Also, by using print statements, I've realized that the request.method is always GET, not POST, which makes it so no model instance is ever saved.  What does this GET request refer to specifically?

Thanks




Auto Generated Inline Image 1

Hugo Osvaldo Barrera

unread,
Aug 25, 2015, 2:05:59 PM8/25/15
to Hugo Kitano, Django users
 
On Tue, Aug 25, 2015, at 14:16, Hugo Kitano wrote:
Noted.  "/stat/s" is an index page.  Here is what my urls.py looks like.  Also, by using print statements, I've realized that the request.method is always GET, not POST, which makes it so no model instance is ever saved.  What does this GET request refer to specifically?
 
Thanks
 
 
 
 

 
Your form posts to /stats/, but /stats/ does not map to your view method `submit`. You need to post data to the view that's processing it, so fix t form or the urls.py.
 
On Monday, August 24, 2015 at 3:29:40 PM UTC-7, Hugo Osvaldo Barrera wrote:
 
On Mon, Aug 24, 2015, at 16:41, Hugo Kitano wrote:
I'm actually now pretty certain that my html file is what's causing it to not work
 

 
Including your message as an inline image rather than simple text will reduce the amount of people that can actually read your message (and complicate following the thread).
 
Does "/stats/" point to the above mentioned view ("submit")? What does your urls.py look like? Did you try putting a "print" (or anything alike) into the view function to really make sure it's the one being called?
 
--
Hugo Osvaldo Barrera
 

Email had 1 attachment:

  • Auto Generated Inline Image 1
      12k (image/png)
 
--
Hugo Osvaldo Barrera
 

Hugo Kitano

unread,
Aug 25, 2015, 2:46:51 PM8/25/15
to Django users, hkit...@gmail.com, hu...@barrera.io
So the form should have action = '/stats/submit/' ? If I do this, whenever I click submit, it brings me back to the submission page, telling me that I didn't submit a file even if I did.

Florian Schweikert

unread,
Aug 31, 2015, 3:15:48 PM8/31/15
to django...@googlegroups.com
On 25/08/15 01:25, Sait Maraşlıoğlu wrote:
> default = timezone.now()

Beware, timezone.now() will be evaluated once when the module is loaded,
not on save if you might think.
Use default = timezone.now without calling it to get the current value
on save.

signature.asc
Reply all
Reply to author
Forward
0 new messages