Discussion:
can't save data to the database
Tosin Ayoola
2018-11-12 13:30:47 UTC
Permalink
halo,
working on a contact us form to receive information from the user but my
issue is the data doesn't get save to the database and i'm not getting any
error message can anyone help out
below is my view an forms.py
--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHLKn70gTCa98GpXiYfm1kJqqQZHYiaYz4U39ocBCuGa4n-3Lw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Andréas Kühne
2018-11-12 14:06:05 UTC
Permalink
Hi,

First of all - are you sure that the form is valid? You don't show the
ContactUs model, but I'm guessing that all fields are required? Are they
all filled in?
Second - in the contact method you first check if it is a POST and then
populate with: form = ContactUsForm(request.POST or None).
The "or None" part won't do anything - it will never be called.
Finally - if the form ISN'T valid - the user won't get any response at all?
It'll just go to en empty page?

Regards,

Andréas
Post by Tosin Ayoola
halo,
working on a contact us form to receive information from the user but my
issue is the data doesn't get save to the database and i'm not getting any
error message can anyone help out
below is my view an forms.py
--
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
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAHLKn70gTCa98GpXiYfm1kJqqQZHYiaYz4U39ocBCuGa4n-3Lw%40mail.gmail.com
<https://groups.google.com/d/msgid/django-users/CAHLKn70gTCa98GpXiYfm1kJqqQZHYiaYz4U39ocBCuGa4n-3Lw%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCfjemsyhhfOiDEB1oXFrPL47ixb_Vvq2624Pwv1ddBeuw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Andréas Kühne
2018-11-12 14:31:37 UTC
Permalink
Just move the return render part from the get request path to always
return. That way you will get the form with the correct information when
the form isn't valid.


Regards,

Andréas
yes the form is valid and the issue is the form doesn't display any error
if the required field are not filled
even when i use just the form = ContactUsForm(request.POST), it still
doesn't work
as for the third wat do you suggest i do ?
Post by Andréas Kühne
Hi,
First of all - are you sure that the form is valid? You don't show the
ContactUs model, but I'm guessing that all fields are required? Are they
all filled in?
Second - in the contact method you first check if it is a POST and then
populate with: form = ContactUsForm(request.POST or None).
The "or None" part won't do anything - it will never be called.
Finally - if the form ISN'T valid - the user won't get any response at
all? It'll just go to en empty page?
Regards,
Andréas
Post by Tosin Ayoola
halo,
working on a contact us form to receive information from the user but my
issue is the data doesn't get save to the database and i'm not getting any
error message can anyone help out
below is my view an forms.py
--
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
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAHLKn70gTCa98GpXiYfm1kJqqQZHYiaYz4U39ocBCuGa4n-3Lw%40mail.gmail.com
<https://groups.google.com/d/msgid/django-users/CAHLKn70gTCa98GpXiYfm1kJqqQZHYiaYz4U39ocBCuGa4n-3Lw%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAK4qSCfjemsyhhfOiDEB1oXFrPL47ixb_Vvq2624Pwv1ddBeuw%40mail.gmail.com
<https://groups.google.com/d/msgid/django-users/CAK4qSCfjemsyhhfOiDEB1oXFrPL47ixb_Vvq2624Pwv1ddBeuw%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCcSp_L%2BNpHCqM4Yf04%2Byftm9t_DBXe05grHEGMTgrrv4w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Andréas Kühne
2018-11-12 14:58:57 UTC
Permalink
No, I meant like this:

def Contact(request):
if request.method == 'POST':
form = ContactUsForm(request.POST)
if form.is_valid():
form.save()
context = {'form': form}

return redirect('contactus.html')
else:
form = ContactUsForm()

return render(request, 'contactus.html', {'form': form})


Also - the redirect should go to a valid django route - What is the route
that you want to redirect to after a successful posting?

Another thing you should also look into is using class based views instead
of function based views, because then you don't need to write as much code
- but get this working first! :-)

Regards,

Andréas
you mean i should have it like this
form = ContactUsForm(request.POST)
form.save()
context = {'form': form}
return redirect('contactus.html')
form = ContactUsForm()
return( 'contactus.html', ) or .....
Post by Andréas Kühne
Just move the return render part from the get request path to always
return. That way you will get the form with the correct information when
the form isn't valid.
Regards,
Andréas
yes the form is valid and the issue is the form doesn't display any
error if the required field are not filled
even when i use just the form = ContactUsForm(request.POST), it still
doesn't work
as for the third wat do you suggest i do ?
On Mon, Nov 12, 2018 at 3:06 PM Andréas KÌhne <
Post by Andréas Kühne
Hi,
First of all - are you sure that the form is valid? You don't show the
ContactUs model, but I'm guessing that all fields are required? Are they
all filled in?
Second - in the contact method you first check if it is a POST and then
populate with: form = ContactUsForm(request.POST or None).
The "or None" part won't do anything - it will never be called.
Finally - if the form ISN'T valid - the user won't get any response at
all? It'll just go to en empty page?
Regards,
Andréas
Den mån 12 nov. 2018 kl 14:31 skrev Tosin Ayoola <
Post by Tosin Ayoola
halo,
working on a contact us form to receive information from the user but
my issue is the data doesn't get save to the database and i'm not getting
any error message can anyone help out
below is my view an forms.py
--
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
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAHLKn70gTCa98GpXiYfm1kJqqQZHYiaYz4U39ocBCuGa4n-3Lw%40mail.gmail.com
<https://groups.google.com/d/msgid/django-users/CAHLKn70gTCa98GpXiYfm1kJqqQZHYiaYz4U39ocBCuGa4n-3Lw%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAK4qSCfjemsyhhfOiDEB1oXFrPL47ixb_Vvq2624Pwv1ddBeuw%40mail.gmail.com
<https://groups.google.com/d/msgid/django-users/CAK4qSCfjemsyhhfOiDEB1oXFrPL47ixb_Vvq2624Pwv1ddBeuw%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAK4qSCcSp_L%2BNpHCqM4Yf04%2Byftm9t_DBXe05grHEGMTgrrv4w%40mail.gmail.com
<https://groups.google.com/d/msgid/django-users/CAK4qSCcSp_L%2BNpHCqM4Yf04%2Byftm9t_DBXe05grHEGMTgrrv4w%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCcJ%3DPEo1kOuK83Nts1cQnKhA6y3jVyYG329wgPE-ZtTkQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...