Further play with django from last week.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 22 Apr 2015 18:49:57 +0000 (20:49 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 22 Apr 2015 18:49:57 +0000 (20:49 +0200)
alpenzoo_django/urls.py
animals/admin.py
animals/models.py
animals/templates/animals/detail.html [new file with mode: 0644]
animals/urls.py [new file with mode: 0644]
animals/views.py

index 6f9aa9c9e94ab58d92864008a5845c1e375f978a..d3e7cf900afba10a1b7510ee22e264c976d6df71 100644 (file)
@@ -1,10 +1,12 @@
 from django.conf.urls import include, url
 from django.contrib import admin
 
+
 urlpatterns = [
     # Examples:
     # url(r'^$', 'alpenzoo_django.views.home', name='home'),
     # url(r'^blog/', include('blog.urls')),
 
+    url(r'^animals/', include('animals.urls')),
     url(r'^admin/', include(admin.site.urls)),
 ]
index 8c38f3f3dad51e4585f3984282c2a4bec5349c1e..618797ec8d4b3b08b33557680860dbbcab1c6b7b 100644 (file)
@@ -1,3 +1,10 @@
 from django.contrib import admin
+from .models import Animal
+
+
+class AnimalAdmin(admin.ModelAdmin):
+    list_display = ('name_german', 'size_cm')
+
 
 # Register your models here.
+admin.site.register(Animal, AnimalAdmin)
\ No newline at end of file
index b9841f954c54403310f2549cfe0cafe5f3d09030..72568d1321e0d7479fa5cc206786a104428b8b07 100644 (file)
@@ -7,4 +7,7 @@ class Animal(models.Model):
     size_cm = models.IntegerField()
     edible = models.BooleanField()
 
+    def __str__(self):
+        return self.name_german
+
 
diff --git a/animals/templates/animals/detail.html b/animals/templates/animals/detail.html
new file mode 100644 (file)
index 0000000..6955411
--- /dev/null
@@ -0,0 +1,4 @@
+<h1>{{ animal.name_german }}</h1>
+<ul>
+    <li>Size: {{ animal.size_cm }}</li>
+</ul>
\ No newline at end of file
diff --git a/animals/urls.py b/animals/urls.py
new file mode 100644 (file)
index 0000000..8969be5
--- /dev/null
@@ -0,0 +1,8 @@
+from django.conf.urls import url
+
+from . import views
+
+urlpatterns = [
+    url(r'^$', views.index, name='index'),
+    url(r'^(?P<animal_id>[0-9]+)/$', views.detail),
+]
index 91ea44a218fbd2f408430959283f0419c921093e..fb5637ea7922929a1c2bb53630cb771e4e3b48c9 100644 (file)
@@ -1,3 +1,13 @@
-from django.shortcuts import render
+from django.shortcuts import get_object_or_404, render
+from django.http import HttpResponse
+from .models import Animal
 
 # Create your views here.
+def index(request):
+    return HttpResponse("Hello, world. You're at the animals index.")
+
+
+def detail(request, animal_id):
+    # return HttpResponse("Hello, world. You're at the animal {}.".format(animal_id))
+    animal = get_object_or_404(Animal, pk=animal_id)
+    return render(request, 'animals/detail.html', {'animal': animal})