Commits

Alex Rattray committed c9bb11e

made the Pages list more automatic

  • Participants
  • Parent commits 94f3a4d

Comments (0)

Files changed (11)

mysite/app/templatetags/__init__.py

Empty file added.

mysite/app/templatetags/__init__.pyc

Binary file added.

mysite/app/templatetags/hackpack_tags.py

+from django import template
+
+register = template.Library()
+
+@register.simple_tag
+def active(request, pattern):
+  import re
+  # if re.search(pattern, request.path):
+  if pattern == request.path:
+    return 'active'
+  return ''

mysite/app/templatetags/hackpack_tags.pyc

Binary file added.

mysite/app/views.py

 #HP app-specific stuff
 from app.models import *
 from app.forms import *
-import settings
+import settings, urls
 
 #HP everything else (these are usually useful)
 import os, sys, datetime, json
 #HP:
 def hackpackify(request, context):
   '''
-    Updates a view's context to include variables expected in base.html
-    Intended to make boilerplate info conveyance and menu bars quick and easy.
-    and returns a RequestContext of the resulting dict (which is usually better).
+  Updates a view's context to include variables expected in base.html
+  Intended to make boilerplate info conveyance and menu bars quick and easy.
+  and returns a RequestContext of the resulting dict (which is usually better).
 
-    CHANGE EVERYTHING IN THIS!
+  CHANGE EVERYTHING IN THIS!
   '''
-  pages = [
-      #HP These pages will appear in the top bar. List does not have to be exhaustive.
-      {'name':'Home', 'url':'/'},
-      {'name':'About', 'url':'/about/'},
-    ]
+  pages = []
+  for urlpat in urls.urlpatterns:
+    if urlpat.__dict__.__contains__('name'):
+      if '(' not in urlpat.regex.pattern:
+        pages.append({'name':urlpat.name, 'url':urlpat.regex.pattern.replace('^','/').replace('$','')})
   #HP project_name is used in navbar, copyright (footer), about page, and <title>
   project_name = "A Django HackPack Project" 
   #HP project_description is used in <meta name="description"> and the about page.

mysite/app/views.pyc

Binary file modified.

mysite/settings.py

 #     'django.template.loaders.eggs.Loader',
 )
 
+#HP added this for the last line, which adds a "request" object to the template variables. 
+TEMPLATE_CONTEXT_PROCESSORS = ("django.contrib.auth.context_processors.auth",
+    "django.core.context_processors.debug",
+    "django.core.context_processors.i18n",
+    "django.core.context_processors.media",
+    "django.core.context_processors.static",
+    "django.contrib.messages.context_processors.messages",
+    "django.core.context_processors.request",
+    )
+
 MIDDLEWARE_CLASSES = (
     'django.middleware.common.CommonMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     # Trust us, you'll need it if you're using a relational database like sqlite, MYSQL, POSTGRES
     # install south with `pip install south` (it comes pre-installed on pythonanywhere)
     # If you're not using a relational database, you can comment this out. 
-    'south',
+    'south', #HP
     'mysite.app' #HP
 )
 

mysite/settings.pyc

Binary file modified.

mysite/templates/base.html

           <a class="brand" href="/">{{hackpack.project_name}}</a>
           <div class="nav-collapse collapse">
             <ul class="nav">
+              {% load hackpack_tags %}
               {% block nav %}
                 {% for page in hackpack.pages %}
-                  {% if page.name == thispage %}
-                    <li class="active"><a href="{{page.url}}">{{page.name}}</a></li>
-                  {% else %}
-                    <li><a href="{{page.url}}">{{page.name}}</a></li>
-                  {% endif %}
+                  <li class="{% active request page.url %}"><a href="{{page.url}}">{{page.name}}</a></li>
                 {% endfor %}
               {% endblock nav %}
             </ul>
     
     #HP the real pages
 
-    (r'^$', views.index),
-    (r'^about/$', views.about),
+    (r'^$', views.index, {}, "Home"),
+    (r'^about/$', views.about, {}, "About"),
     #HP add pages here
 )

mysite/urls.pyc

Binary file modified.