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
--
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.
The code seems right. Probably your form has some error. Try print {{form.errors}} in your template to check.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGjPPHn0f-rdRKDd4J9sA%3DVbH4R7zTvyPmMYv-dCb80%3DiLq%2Bsg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
>>>> 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
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
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
I'm actually now pretty certain that my html file is what's causing it to not work
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
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 workIncluding 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)