title: How use TemplateView with 2 methods (get and post)
body: <p>I am trying to use Templateview in Django to render a page with options for both adding to the database and retrieving some info from the database and displaying it. I am basing it on the tutorial at <a href="https://www.youtube.com/watch?v=VxOsCKMStuw" rel="nofollow noreferrer">https://www.youtube.com/watch?v=VxOsCKMStuw</a></p>
  
  <p><strong>views.py:</strong></p>
  
  <pre><code>class TestView(TemplateView):
      template_name = 'app/sensor_name_tmpl.html'
  
      def get(self, request):
          form = SensorForm()
          posts = Sensor.objects.all()
  
          args = {'form': form, 'posts': posts}
          return render(request, self.template_name, args)
  
  
      def post(self, request):
          form = SensorForm(request.POST)
          if form.is_valid():
              form.save()
              text = form.cleaned_data['post']
              form = SensorForm()
              return redirect('sensor_name_tmpl:sensor_name_tmpl')
  
          args = {'form': form, 'text': text}
          return render(request, self.template_name, args)
  </code></pre>
  
  <p><strong>urls.py:</strong></p>
  
  <pre><code>urlpatterns = [
      path('', views.index, name='index'),
      url(r'^form1/$', views.get_sensor_name, name='GiveSensorName1'),
      #url(r'^form2/$', TestView.as_view(), name='sensor_name_tmpl.html'),
      path('form2/', TestView.as_view(), name='app/sensor_name_tmpl.html'),
      url(r'^nested_admin/', include('nested_admin.urls')),
  ]
  </code></pre>
  
  <p><strong>HTML template:</strong></p>
  
  <pre><code>    &lt;!doctype html&gt;
  &lt;html lang="en"&gt;
    &lt;head&gt;
      &lt;!-- Required meta tags --&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"&gt;
      &lt;script src="https://code.jquery.com/jquery-1.10.2.js"&gt;&lt;/script&gt;
          &lt;script&gt;
              $('#toggle').click(function() {
              $('form').toggle('slow');
              });
          &lt;/script&gt;
      &lt;!-- Bootstrap CSS --&gt;
      &lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"&gt;
  
      &lt;title&gt;Hello world!&lt;/title&gt;
    &lt;/head&gt;
  
  
    &lt;body&gt;
      &lt;h3 class="text-success"&gt;Add Sensor&lt;/h3&gt;
      &lt;br&gt;
  
  &lt;!--    &lt;form style="display:none;" method="post"&gt;--&gt;
          &lt;form method="post"&gt;
          {% csrf_token %}
          &lt;div class="row align-items-center"&gt;
              &lt;div class="col-sm-8"&gt;
                  &lt;table&gt;
                     {{ form1.as_table}}
                  &lt;/table&gt;
                  &lt;div class="mx-sm-2"&gt;
                      &lt;input type="submit" value="Submit"&gt;
                  &lt;/div&gt;
  
                  &lt;br&gt;
                  &lt;br&gt;
  
                  &lt;h3 class = "text-success"&gt;Add Sensor View&lt;/h3&gt;
                  &lt;table&gt;
                     {{ form2.as_table}}
                  &lt;/table&gt;
                    &lt;div class="mx-sm-2"&gt;
                          &lt;input type="submit" value="Submit"&gt;
                    &lt;/div&gt;
                  &lt;br&gt;
                  &lt;br&gt;
  
                  &lt;h3 class="text-success"&gt;View Sensors&lt;/h3&gt;
  
                  &lt;table class="table"&gt;
                      &lt;thead&gt;
                          &lt;tr&gt;
                              &lt;th scope="col"&gt;Sensor ID&lt;/th&gt;
                              &lt;th scope="col"&gt;Sensor Name&lt;/th&gt;
                          &lt;/tr&gt;
                      &lt;/thead&gt;
                  &lt;tbody&gt;
                  {%for obj in obj%}
                      &lt;tr&gt;
                          &lt;td&gt;{{obj.sensor_id}}&lt;/td&gt;
                          &lt;td&gt;{{obj.sensor_name}}&lt;/td&gt;
  &lt;!--                        &lt;th scope="row"&gt;1&lt;/th&gt;--&gt;
                      &lt;/tr&gt;
                  {% endfor %}
                  &lt;/tbody&gt;
                  &lt;/table&gt;
  
  
  
  
  
              &lt;/div&gt;
          &lt;div&gt;
      &lt;/form&gt;
  
  
      &lt;!-- Optional JavaScript --&gt;
      &lt;!-- jQuery first, then Popper.js, then Bootstrap JS --&gt;
  
      &lt;script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"&gt;&lt;/script&gt;
      &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"&gt;&lt;/script&gt;
  
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The page renders the template but doesn't populate it with either the formfields or the data from the database.</p>