Browse Source

fortune test for django

Patrick Falls 12 years ago
parent
commit
d6e634143d

+ 1 - 0
django/hello/hello/urls.py

@@ -16,4 +16,5 @@ urlpatterns = patterns('',
     # url(r'^admin/', include(admin.site.urls)),
     url(r'^json$', 'world.views.json'),
     url(r'^db$', 'world.views.db'),
+    url(r'^fortunes$', 'world.views.fortunes'),
 )

+ 1 - 4
django/hello/templates/base.html

@@ -2,10 +2,7 @@
 <html>
 <head>
 </head>
-
 <body>
-  <div id="content">
-    {% block content %}{% endblock %}
-  </div>
+  {% block content %}{% endblock %}
 </body>
 </html>

+ 16 - 0
django/hello/templates/fortunes/index.html

@@ -0,0 +1,16 @@
+{% extend base.html %}
+
+{% block content %}
+<table>
+<tr>
+<th>id</th>
+<th>message</th>
+</tr>
+{% for fortune in fortunes %}
+<tr>
+<td>{% fortune.id %}</td>
+<td>{% fortune.message %}</td>
+</tr>
+{% endfor %}
+</table>
+{% endblock %}

+ 4 - 0
django/hello/world/models.py

@@ -7,3 +7,7 @@ class World(models.Model):
   class Meta:
     db_table = 'world'
 
+class Fortune(models.Model):
+  message = models.StringField()
+  class Meta:
+    db_table = 'fortune'

+ 9 - 0
django/hello/world/views.py

@@ -4,6 +4,7 @@ from django.template import Context, loader
 from django.http import HttpResponse
 from django.core import serializers
 from world.models import World
+from world.models import Fortune
 import ujson
 import random
 
@@ -23,3 +24,11 @@ def db(request):
 
   return HttpResponse(serializers.serialize("json", worlds), mimetype="application/json")
 
+def fortunes(request):
+  fortunes = Fortune.objects.all()
+  fortunes.append(Fortune(id=0, message="Additional message added at runtime."))
+
+  fortunes = sorted(fortunes, key=attrgetter('message'))
+
+  context = {'fortunes': fortunes}
+  return render(request, 'fortunes/index.html', context)