[Django]-Django form not visible

4👍

You should give the if form.is_valid() part, one more tab.

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from compare.forms import NameForm

def get_name(request):
   if request.method == 'POST':
      form = NameForm(request.POST)
      if form.is_valid():
          return HttpResponseRedirect('/thanks/')

   else:
      form = NameForm()

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

1👍

I’m not yet allowed to add comments to your question, but you did notice, that your view renders name.html while you’re providing the code for index.htm.

Basically your code seems valid, while it can be further optimized:

def get_name(request):
    form = NameForm(request.POST or None)

    if form.is_valid():
        return HttpResponseRedirect('/thanks/')

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

1👍

 <form action="/your-name/" method="post">
            {% csrf_token %}
            {{ form.as_p }}
        <input type="submit" value="Submit" />
        </form>

This should work by adding editing {{form.as_p}}

Leave a comment