3

I'm reading the documentation on how to display a message to the user with Django messages. It says that in order to add a message in your views to call:

from django.contrib import messages
messages.add_message(request, messages.INFO, 'Hello world.')

I'm not sure where to put the second the second line, this is my view:

def sign_up(request):

    if request.method == "POST":
        form = IdForm(request.POST)
        if form.is_valid():
            post = form.save()
            post.save()
            ID = post.id_text
            return HttpResponse('Thank you')
        else:
            return HttpResponse('That text is invalid')
    else:
        form = IdForm()
    return render(request, 'checkin/base.html', {'form': form})

I want the message to appear and thank the user for signing up and display their input as well.

e4c5
  • 52,766
  • 11
  • 101
  • 134
Amon
  • 2,725
  • 5
  • 30
  • 52

2 Answers2

3

Bearing, in mind that it's customary to redirect to a success url on valid form submission, your code ought to look like this:

def sign_up(request):

    if request.method == "POST":
        form = IdForm(request.POST)
        if form.is_valid():
            post = form.save()
            post.save()
            ID = post.id_text
            messages.add_message(request, messages.INFO, 'Hello world.')
            return HttpResponseRedirect('/thank-you-page/')

    else:
        form = IdForm()
    return render(request, 'checkin/base.html', {'form': form})

note that this also results in the user being told why exactly his form is invalid (assuming that you have set up the template propertly). It's always good to say what the problem is rather than to say there is a problem.

e4c5
  • 52,766
  • 11
  • 101
  • 134
  • When is the message supposed to appear? When you arrive at the page that your'e redirected to? Because when I pass the path to `HttpResponseRedirect` I'm getting the 404 error. My app URL is `localhost:8000/checkin/` with the view being located at `localhost:8000/checkin/sign_up` however when I try to give the argument '/sign_in/` (lets just say I redirect them to the page where you sign in) to `HttpResponseRedirect` it's not redirecting me to `localhost:8000/checkin/sign_in` it's redirecting me to `localhost:8000/sign_up` instead. – Amon Nov 11 '16 at 05:48
  • 1
    '/thank-you-page/' was just an example, please choose the most appropriate page on your site. It's pointless to redirect the user to the sign in page again. Usually most sites have a dash board or home screen where the user is sent to after login – e4c5 Nov 11 '16 at 05:50
  • But would that fix the problem of where it's redirecting to? I'll write another view and check back – Amon Nov 11 '16 at 06:33
  • Okay it seems I've fixed the issue with `HttpResponseRedirect`. The problem was that I was including `/` in the string. However the Django message still isn't appearing – Amon Nov 11 '16 at 06:37
  • 1
    that's a different issue. Probably becacuse you haven't enabled it the settings or in the templates. https://docs.djangoproject.com/en/1.10/ref/contrib/messages/ . May i suggest that you post another question on it (please include the relevent section of the settings) – e4c5 Nov 11 '16 at 06:40
1

You can put the second line in view with example:

def contact(request):

if request.method == 'POST':
    name = request.POST.get('name')
    email = request.POST.get('email')
    password = request.POST.get('password')
    textarea = request.POST.get('textarea')
    contact = Contact(name = name,email = email,password = password,textarea = textarea,date=datetime.today())
    contact.save()
    messages.success(request, 'Form has submitted')

return render(request,"contact.html")