Browse Source

Refactored: HP view to use a template.

david 5 years ago
parent
commit
1f17db0546
5 changed files with 14 additions and 8 deletions
  1. 3 0
      lists/templates/home.html
  2. 9 4
      lists/tests.py
  3. 1 4
      lists/views.py
  4. BIN
      refactoring_cat.gif
  5. 1 0
      superlists/settings.py

+ 3 - 0
lists/templates/home.html

@@ -0,0 +1,3 @@
+<html>
+    <title>To-Do lists</title>
+</html>

+ 9 - 4
lists/tests.py

@@ -1,6 +1,7 @@
 from django.urls import resolve
 from django.test import TestCase
 from django.http import HttpRequest
+from django.template.loader import render_to_string
 
 from lists.views import home_page
 
@@ -13,9 +14,13 @@ class HomePageTest(TestCase):
 
 
     def test_home_page_returns_correct_html(self):
-        request = HttpRequest()
-        response = home_page(request)
-        html = response.content.decode('utf8')
+        response = self.client.get('/')
+
+        # The Manual Way of doing it:
+        html = response.content.decode('utf8')  
         self.assertTrue(html.startswith('<html>'))
         self.assertIn('<title>To-Do lists</title>', html)
-        self.assertTrue(html.endswith('</html>'))
+        self.assertTrue(html.strip().endswith('</html>'))
+
+        # Using Django's Builtin Testing Client:
+        self.assertTemplateUsed(response, 'home.html')

+ 1 - 4
lists/views.py

@@ -1,7 +1,4 @@
 from django.shortcuts import render
-from django.http import HttpResponse
 
-
-# Create your views here.
 def home_page(request):
-    return HttpResponse('<html><title>To-Do lists</title></html>')
+    return render(request, 'home.html')

BIN
refactoring_cat.gif


+ 1 - 0
superlists/settings.py

@@ -31,6 +31,7 @@ ALLOWED_HOSTS = []
 # Application definition
 
 INSTALLED_APPS = [
+    'lists',
     'django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',