# HG changeset patch # User Branko Majic # Date 2013-02-26 20:44:56 # Node ID b38bff5ff039b29b787004bee2c9e340c9802454 # Parent 603e91a6b1abaabe3c791c0da094b4b46f3350a5 Created the initial skeleton for templates. Added some custom template tags. Created first dummy index view. Configured the test project to use the django.core.context_processors.request template context processor (needed for active links detection). diff --git a/conntrackt/templates/conntrackt/index.html b/conntrackt/templates/conntrackt/index.html new file mode 100644 --- /dev/null +++ b/conntrackt/templates/conntrackt/index.html @@ -0,0 +1,6 @@ +{% extends "conntrackt/template.html" %} + +{% block content %} +

Welcome to Conntrackt

+{% endblock %} + diff --git a/conntrackt/templates/conntrackt/link.html b/conntrackt/templates/conntrackt/link.html new file mode 100644 --- /dev/null +++ b/conntrackt/templates/conntrackt/link.html @@ -0,0 +1,7 @@ +{% load url from future %} + +{% if identifier %} +{{title}} +{% else %} +{{title}} +{% endif %} diff --git a/conntrackt/templates/conntrackt/template.html b/conntrackt/templates/conntrackt/template.html new file mode 100644 --- /dev/null +++ b/conntrackt/templates/conntrackt/template.html @@ -0,0 +1,52 @@ +{% load conntrackt %} + + + + + {% block title %}Conntrackt{% endblock %} + + + + + + + + + + + +
+
+ {% block content %} + {% endblock %} +
+
+ + + + + + diff --git a/conntrackt/templatetags/__init__.py b/conntrackt/templatetags/__init__.py new file mode 100644 diff --git a/conntrackt/templatetags/conntrackt.py b/conntrackt/templatetags/conntrackt.py new file mode 100644 --- /dev/null +++ b/conntrackt/templatetags/conntrackt.py @@ -0,0 +1,44 @@ +from django import template +from django.core import urlresolvers + +register = template.Library() + +@register.inclusion_tag('conntrackt/link.html') +def html_link(view, title, identifier = None, html_class = None): + ''' + A small wrapper for showing HTML links. + + Arguments: + + view - View name for which the URL should be shown. + + title - Title which will be shown as the link. + + identifier - Identifier which is passed to the view for processing. + + html_class - Class which should be assigned to the link. + ''' + + return {'view': view, 'identifier': identifier, 'title': title, 'html_class': html_class} + +@register.simple_tag(takes_context = True) +def active_link(context, url_name, return_value='active', **kwargs): + matches = current_url_equals(context, url_name, **kwargs) + return return_value if matches else '' + + +def current_url_equals(context, url_name, **kwargs): + resolved = False + try: + resolved = urlresolvers.resolve(context.get('request').path) + except: + pass + matches = resolved and resolved.url_name == url_name + if matches and kwargs: + for key in kwargs: + kwarg = kwargs.get(key) + resolved_kwarg = resolved.kwargs.get(key) + if not resolved_kwarg or kwarg != resolved_kwarg: + return False + return matches + diff --git a/conntrackt/urls.py b/conntrackt/urls.py --- a/conntrackt/urls.py +++ b/conntrackt/urls.py @@ -1,6 +1,9 @@ from django.conf.urls import patterns, include, url +from conntrackt.views import IndexView + urlpatterns = patterns( 'threadedcomments_zinnia.views', + url(r'^$', IndexView.as_view(), name="index"), ) diff --git a/conntrackt/views.py b/conntrackt/views.py --- a/conntrackt/views.py +++ b/conntrackt/views.py @@ -1,1 +1,9 @@ -# Create your views here. +from django.views.generic import TemplateView + +class IndexView(TemplateView): + """ + Custom view used for rendering the index page. + """ + + template_name = 'conntrackt/index.html' + diff --git a/projtest/projtest/settings.py b/projtest/projtest/settings.py --- a/projtest/projtest/settings.py +++ b/projtest/projtest/settings.py @@ -1,5 +1,4 @@ # Django settings for projtest project. - DEBUG = True TEMPLATE_DEBUG = DEBUG @@ -108,6 +107,13 @@ TEMPLATE_DIRS = ( # Don't forget to use absolute paths, not relative paths. ) +# Extend the default TEMPLATE_CONTEXT_PROCESSORS +from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS +TEMPLATE_CONTEXT_PROCESSORS += ( + "django.core.context_processors.request", + ) + + INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes',