diff --git a/README.rst b/README.rst
index de391e5..1abf9f9 100644
--- a/README.rst
+++ b/README.rst
@@ -19,18 +19,32 @@ Installation & Configuration:
3. ``./manage.py migrate menu`` (or ``./manage.py syncdb`` if you don't use South. You should use South.)
-4. Add ``django.core.context_processors.request`` to your ``TEMPLATE_CONTEXT_PROCESSORS``. It is not there by default. The default ``TEMPLATE_CONTEXT_PROCESSORS`` are:::
-
- "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"
+4. Add ``django.template.context_processors.request`` to your ``TEMPLATE`` settings. Below is a reasonably safe ``TEMPLATES`` setting for most small projects, however yours may vary.:
+
+ .. code-block:: python
+
+ TEMPLATES = [
+ {
+ 'BACKEND': 'django.template.backends.django.DjangoTemplates',
+ 'DIRS': [os.path.join(BASE_DIR, 'templates')],
+ 'APP_DIRS': True,
+ 'OPTIONS': {
+ 'context_processors': [
+ 'django.template.context_processors.debug',
+ 'django.template.context_processors.request',
+ 'django.contrib.auth.context_processors.auth',
+ 'django.contrib.messages.context_processors.messages',
+ 'django.template.context_processors.request',
+ ],
+ },
+ },
+ ]
5. Add a Menu (eg called ``headernavigation``) and add some items to that menu
-6. In your template, load the menu tags and embed your primary menu.:::
+6. In your template, load the menu tags and embed your primary menu.
+
+ .. code-block:: html+django
{% load menubuilder %}{% menu headernavigation %}
{% for item in menuitems %}- {{ item.title }}
diff --git a/menu/__init__.py b/menu/__init__.py
index e69de29..bf1ffef 100644
--- a/menu/__init__.py
+++ b/menu/__init__.py
@@ -0,0 +1,3 @@
+# -*- coding: utf-8 -*-
+
+default_app_config = 'menu.apps.AppConfig'
diff --git a/menu/apps.py b/menu/apps.py
new file mode 100644
index 0000000..7793166
--- /dev/null
+++ b/menu/apps.py
@@ -0,0 +1,14 @@
+# -*- coding: utf-8 -*-
+
+try:
+ from django.utils.translation import ugettext_lazy as _
+except ImportError:
+ from django.utils.translation import gettext_lazy as _
+
+from django.apps import AppConfig as BaseConfig
+
+
+class AppConfig(BaseConfig):
+ name = 'menu'
+ verbose_name = _('Menu')
+
diff --git a/menu/locale/zh_Hans/LC_MESSAGES/django.mo b/menu/locale/zh_Hans/LC_MESSAGES/django.mo
new file mode 100644
index 0000000..3150fff
Binary files /dev/null and b/menu/locale/zh_Hans/LC_MESSAGES/django.mo differ
diff --git a/menu/locale/zh_Hans/LC_MESSAGES/django.po b/menu/locale/zh_Hans/LC_MESSAGES/django.po
new file mode 100644
index 0000000..c911a59
--- /dev/null
+++ b/menu/locale/zh_Hans/LC_MESSAGES/django.po
@@ -0,0 +1,88 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR , YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2016-05-22 22:05+0800\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: Wang WenPei \n"
+"Language-Team: LANGUAGE \n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n"
+"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)\n"
+
+#: menu/apps.py:8 menu/apps.py:9
+msgid "Menu"
+msgstr "菜单项"
+
+#: menu/models.py:7 menu/models.py:57
+msgid "Name"
+msgstr "名称"
+
+#: menu/models.py:12
+msgid "Slug"
+msgstr "菜单组标签"
+
+#: menu/models.py:16
+msgid "Base URL"
+msgstr "基础URL"
+
+#: menu/models.py:23
+msgid "Description"
+msgstr "描述"
+
+#: menu/models.py:29
+msgid "menu"
+msgstr "菜单组"
+
+#: menu/models.py:30
+msgid "menus"
+msgstr "菜单组"
+
+#: menu/models.py:61
+msgid "Order"
+msgstr "排序"
+
+#: menu/models.py:66
+msgid "Link URL"
+msgstr "链接地址"
+
+#: menu/models.py:68
+msgid "URL or URI to the content, eg /about/ or http://foo.com/"
+msgstr "支持站内链接或站外链接, 比如: /about/ 或 http://foo.com/"
+
+#: menu/models.py:72
+msgid "Title"
+msgstr "菜单标题"
+
+#: menu/models.py:77
+msgid "Login required"
+msgstr "需要登录"
+
+#: menu/models.py:79
+msgid "Should this item only be shown to authenticated users?"
+msgstr "设定当前菜单只在登录时才显示"
+
+#: menu/models.py:83
+msgid "Anonymous only"
+msgstr "只在匿名时显示"
+
+#: menu/models.py:85
+msgid "Should this item only be shown to non-logged-in users?"
+msgstr "设定当前菜单只在未登录时才显示"
+
+#: menu/models.py:89
+msgid "menu item"
+msgstr "菜单"
+
+#: menu/models.py:90
+msgid "menu items"
+msgstr "菜单"
diff --git a/menu/migrations/0002_booleandefaults.py b/menu/migrations/0002_booleandefaults.py
new file mode 100644
index 0000000..58704b9
--- /dev/null
+++ b/menu/migrations/0002_booleandefaults.py
@@ -0,0 +1,25 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.2 on 2016-10-12 03:55
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('menu', '0001_initial'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='menuitem',
+ name='anonymous_only',
+ field=models.BooleanField(default=False, help_text='Should this item only be shown to non-logged-in users?', verbose_name='Anonymous only'),
+ ),
+ migrations.AlterField(
+ model_name='menuitem',
+ name='login_required',
+ field=models.BooleanField(default=False, help_text='Should this item only be shown to authenticated users?', verbose_name='Login required'),
+ ),
+ ]
diff --git a/menu/models.py b/menu/models.py
index ef1b7d5..0de08c8 100644
--- a/menu/models.py
+++ b/menu/models.py
@@ -1,4 +1,8 @@
-from django.utils.translation import ugettext_lazy as _
+try:
+ from django.utils.translation import ugettext_lazy as _
+except ImportError:
+ from django.utils.translation import gettext_lazy as _
+
from django.db import models
@@ -54,7 +58,8 @@ def save(self, *args, **kwargs):
class MenuItem(models.Model):
menu = models.ForeignKey(
Menu,
- verbose_name=_(u'Name')
+ verbose_name=_(u'Name'),
+ on_delete=models.CASCADE,
)
order = models.IntegerField(
@@ -76,12 +81,14 @@ class MenuItem(models.Model):
login_required = models.BooleanField(
_(u'Login required'),
blank=True,
+ default=False,
help_text=_(u'Should this item only be shown to authenticated users?')
)
anonymous_only = models.BooleanField(
_(u'Anonymous only'),
blank=True,
+ default=False,
help_text=_(u'Should this item only be shown to non-logged-in users?')
)
diff --git a/menu/templatetags/menubuilder.py b/menu/templatetags/menubuilder.py
index 971bfb8..96fdb75 100644
--- a/menu/templatetags/menubuilder.py
+++ b/menu/templatetags/menubuilder.py
@@ -22,8 +22,13 @@ def __init__(self, menu_name):
self.menu_name = menu_name
def render(self, context):
- current_path = context['request'].path
- user = context['request'].user
+ try:
+ current_path = context['request'].path
+ user = context['request'].user
+ except KeyError:
+ current_path = None
+ user = None
+
context['menuitems'] = get_items(self.menu_name, current_path, user)
return ''
@@ -66,25 +71,37 @@ def get_items(menu_name, current_path, user):
cache_time = getattr(settings, 'MENU_CACHE_TIME', 1800)
debug = getattr(settings, 'DEBUG', False)
+ if user:
+ is_authenticated = user.is_authenticated
+ is_anonymous = user.is_anonymous
+ else:
+ is_authenticated = False
+ is_anonymous = True
+
if cache_time >= 0 and not debug:
- cache_key = 'django-menu-items/%s/%s/%s' % (menu_name, current_path, user.is_authenticated())
+ cache_key = 'django-menu-items/%s/%s/%s' % (menu_name, current_path, is_authenticated)
menuitems = cache.get(cache_key, [])
if menuitems:
return menuitems
else:
menuitems = []
- try:
- menu = Menu.objects.get(slug=menu_name)
- except Menu.DoesNotExist:
+
+ menu = Menu.objects.filter(slug=menu_name).first()
+
+ if not menu:
return []
for i in MenuItem.objects.filter(menu=menu).order_by('order'):
- current = ( i.link_url != '/' and current_path.startswith(i.link_url)) or ( i.link_url == '/' and current_path == '/' )
- if menu.base_url and i.link_url == menu.base_url and current_path != i.link_url:
- current = False
- show_anonymous = i.anonymous_only and user.is_anonymous()
- show_auth = i.login_required and user.is_authenticated()
+ if current_path:
+ current = ( i.link_url != '/' and current_path.startswith(i.link_url)) or ( i.link_url == '/' and current_path == '/' )
+ if menu.base_url and i.link_url == menu.base_url and current_path != i.link_url:
+ current = False
+ else:
+ current =False
+
+ show_anonymous = i.anonymous_only and is_anonymous
+ show_auth = i.login_required and is_authenticated
if (not (i.login_required or i.anonymous_only)) or (i.login_required and show_auth) or (i.anonymous_only and show_anonymous):
menuitems.append({'url': i.link_url, 'title': i.title, 'current': current,})
diff --git a/setup.py b/setup.py
index 6b8766c..84c4dba 100644
--- a/setup.py
+++ b/setup.py
@@ -4,7 +4,7 @@
from fnmatch import fnmatchcase
from setuptools import setup, find_packages
-version = '0.1.9'
+version = '0.1.13'
# Provided as an attribute, so you can append to these instead
# of replicating them:
@@ -123,7 +123,7 @@ def find_package_data(
keywords=['django', 'menus', 'navigatino'],
author='Ross Poulton',
author_email='ross@rossp.org',
- url='http://github.com/rossp/django-menu',
+ url='https://github.com/rossp/django-menu',
license='BSD',
packages=find_packages(),
package_data=find_package_data("menu", only_in_packages=False),