can't save data to the database

105 views
Skip to first unread message

Tosin Ayoola

unread,
Nov 12, 2018, 8:31:45 AM11/12/18
to django...@googlegroups.com
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
Screenshot from 2018-11-12 13-43-00.png
Screenshot from 2018-11-12 13-43-38.png

Andréas Kühne

unread,
Nov 12, 2018, 9:06:48 AM11/12/18
to django...@googlegroups.com
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


--
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

unread,
Nov 12, 2018, 9:32:38 AM11/12/18
to django...@googlegroups.com
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


Den mån 12 nov. 2018 kl 15:20 skrev Tosin Ayoola <tosina...@gmail.com>:
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 ?

Andréas Kühne

unread,
Nov 12, 2018, 9:59:34 AM11/12/18
to django...@googlegroups.com
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


Den mån 12 nov. 2018 kl 15:38 skrev Tosin Ayoola <tosina...@gmail.com>:
you mean i should have it 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( 'contactus.html', ) or .....


Reply all
Reply to author
Forward
0 new messages