title: DJANGO - Redirect to different page from POST with data
body: <p>I am trying to have a simple form that once filled, will direct to a different webpage or remain on the same page if invalid. The page should have a text box and submit form and once a user enters anything it should direct you to a separate page. </p>
  
  <p>My directory structure is as follows:</p>
  
  <pre><code>appName/
         app/
            forms.py
            urls.py
            views.py
            templates/
                 app/
                   goodbye.html
                   name.html
                   library.html
                   thanks.html
         appName/
             settings.py
             urls.py
  </code></pre>
  
  <p>My <strong>app/urls.py</strong> is as follows:</p>
  
  <pre><code>from django.conf.urls import url
  from . import views
  
  app_name = 'app'
  
  urlpatterns = [
          url(r'^$', views.index2, name = 'index'),
          url(r'^hello/$', views.hello, name = 'hello'),
          url(r'^goodbye/$', views.goodbye, name = 'goodbye'),
          #url(r'^library$', views.library, name = 'library'),
          url(r'^library/$', views.library, name = 'library'),
          url(r'^library/(?P&lt;book_id&gt;[0-9]+)/$', views.book, name = 'book'),
          url(r'^getname/$', views.get_name, name = 'get_name'),
          url(r'^your-name/$',views.get_name, name='get_name'),
          url(r'^thanks/$',views.say_thanks,name='thanks'),
          #url(r'^thanks/(?P&lt;name_id&gt;[a-zA-Z]+)/$', views.say_thanks,name='thanks'),
          ]
  </code></pre>
  
  <p>My <strong>forms.py</strong> is :</p>
  
  <p>from django import forms</p>
  
  <pre><code>class NameForm(forms.Form):
      your_name = forms.CharField(label = 'Your name', max_length=100)
  </code></pre>
  
  <p>My <strong>app/views.py</strong> is:</p>
  
  <pre><code>from django.http import HttpResponse
  from django.template import loader
  from django.shortcuts import render
  from django.http import HttpResponseRedirect
  
  #forms
  from .forms import NameForm
  
  # Create your views here.
  def index2(request):
      return HttpResponse("hello world")
  
  def hello(request):
      text = """&lt;h1&gt;Welcome to my app! &lt;/h1&gt;"""
      return HttpResponse(text)
  
  def goodbye(request):
      template = loader.get_template("app/goodbye.html")
      context = {
          'output' : 'This is output from goodby views.py request handler'
          }
  
      return HttpResponse(template.render(context,request))
  
  def library(request):
      template = loader.get_template("app/library.html")
      context = {
          'output' : 'Welcome to the libary!!'
          }
      return HttpResponse(template.render(context, request))
  
  def book(request, book_id):
      return HttpResponse("You're looking at book %s. " % book_id)
  
  def get_name(request):
  
      # if this is a POST request we need to process the form data
      if request.method == 'POST':
          # create a form instance and populate it with data from the request:
          form = NameForm(request.POST)
          # check whether it's valid:
          if form.is_valid():
              #process the data in form.cleaned_data as required
              locationGo = "/thanks/"
              template = loader.get_template("app/thanks.html")
  
              return HttpResponse(template.render({'name':'name'},request))
      else:
              form = NameForm()
              template = loader.get_template("app/name.html")
              context = {'form': form}
              return HttpResponse(template.render(context, request))
  
  def say_thanks(request):
      template = loader.get_template("app/thanks.html")
      return HttpResponse(template.render({'name': 'name'},request))
  </code></pre>
  
  <p>My templates include:</p>
  
  <p><strong>name.html</strong> : </p>
  
  <pre><code>&lt;form action = "/getname/" method = "post"&gt;
      {% csrf_token %}
      {{ form }}
      &lt;input type = "submit" value = "Submit" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>goodbye.html</strong></p>
  
  <pre><code>&lt;h1&gt;Goodbye to Template Romance&lt;/h1&gt;
  
  &lt;a href="{% url 'app:index' %}"&gt;Go Back&lt;/a&gt;
  </code></pre>
  
  <p><strong>thanks.html</strong></p>
  
  <pre><code>Thanks {{name}}!
  </code></pre>
  
  <p>What I would <em>like</em> is for:</p>
  
  <ol>
  <li><p>A user to  visit to : website.com/getname/ to show the name.html file (which it does)</p></li>
  <li><p>If a user hits submit to stay on the same page (website.com/getname/) (which it doesn't - it gives: ValueError at /getname/ ->The view app.views.get_name didn't return an HttpResponse object. It returned None instead.</p></li>
  <li><p>If a user enters in the submit field, to be redirected to website.com/thanks/ (which it sort of does. It currently loads the thanks.html template, but the URL stays on website.com/getname/)</p></li>
  </ol>
  
  <p>Inside the get_name(request): function, the POST and GET if...else doesn't seem to be firing based on the Submit button, and it doesn't seem to be loading the correct page, OR change the current URL address once it gets processed. I have tried using HttpRedirect() which works, however, I would also like to pass the forms data (which is another issue). </p>
  
  <p>Any suggestions would be a big help! </p>