first use of templates
authorSven Arnold <sven@internetallee.de>
Mon, 8 Feb 2016 16:19:40 +0000 (17:19 +0100)
committerSven Arnold <sven@internetallee.de>
Mon, 8 Feb 2016 16:19:40 +0000 (17:19 +0100)
project/books/templates/books/index.html [new file with mode: 0644]
project/books/views.py

diff --git a/project/books/templates/books/index.html b/project/books/templates/books/index.html
new file mode 100644 (file)
index 0000000..2b440e1
--- /dev/null
@@ -0,0 +1,9 @@
+{% if book_list %}
+       <ul>
+               {% for book in book_list %}
+               <li>{{ book }}</li>
+               {% endfor %}
+       </ul>
+{% else %}
+       Keine Bücher im Regal!
+{% endif %}
index aa62cca..1f7be40 100644 (file)
@@ -1,11 +1,17 @@
 from django.shortcuts import render
 from django.http import HttpResponse
+from django.template import RequestContext, loader
 
 from books.models import Book, Lease
 
 def index(request):
-       books = ", ".join([str(b) for b in Book.objects.all()])
-       return HttpResponse(books)
+       book_list = Book.objects.all()
+       template = loader.get_template('books/index.html')
+       context = RequestContext(request, {
+               'book_list': book_list,
+       })
+       return HttpResponse(template.render(context))
+       
 
 def detail(request, book_id):
        response = "Book ID: %s"