GithubHelp home page GithubHelp logo

pragmaticmates / django-flatpages-i18n Goto Github PK

View Code? Open in Web Editor NEW
21.0 5.0 25.0 165 KB

Translatable version of django.contrib.flatpages

License: BSD 3-Clause "New" or "Revised" License

Python 95.85% HTML 4.15%

django-flatpages-i18n's Introduction

django-flatpages-i18n

Translatable version of django.contrib.flatpages with menu support.

Requirements

  • Django
  • django_modeltrans
  • django_mptt
  • django-pragmatic

Tested with Django 1.8.

Installation

  1. Install python library using pip: pip install django-flatpages-i18n
  2. Add mptt, modeltrans and flatpages_i18n to INSTALLED_APPS in your Django settings file
  3. Migrate your database
  4. Specify desired languages in your Django settings file:

    from django.utils.translation import gettext
    
    LANGUAGE_CODE = 'en'
    LANGUAGES = (
        ('en', gettext('English')),
        ('de', gettext('German')),
    )
  5. Addd 'flatpages_i18n.urls' to your urls.py:

    if 'flatpages_i18n' in settings.INSTALLED_APPS:
        urlpatterns += i18n_patterns(
            path(pgettext_lazy('url', 'pages/'), include('flatpages_i18n.urls')),
        )

Usage

To get all flatpages:

In your HTML template:

{% load i18n flatpages_i18n %}
{% get_flatpages_i18n as flatpages_i18n %}

<ul>
    {% for flatpage in flatpages_i18n %}
        <li><a href="{{ flatpage.get_absolute_url }}">{{ flatpage }}</a></li>
    {% endfor %}
</ul>

To get flatpage by its PK:

{% get_flatpage_i18n 123 as my_flatpage %}
{{ my_flatpage.content_i18n }}

or by its machine_name:

{% get_flatpage_i18n 'my-flatpage' as my_flatpage %}

Menu system:

To print all menu items:

<div id="navigation">
    {% menu_i18n %}
</div>

to get only children of menu item identified by its PK:

<div id="navigation">
    {% menu_i18n 2 %}
</div>

or by its machine_name:

<div id="navigation">
    {% menu_i18n 'footer-menu' %}
</div>

You can also assign menu items into variable:

{% get_menu_i18n 'my-menu' as my_menu %}
{% for item in my_menu %}
    <a href="{{ item.get_absolute_url }}" target="{{ item.target }}">{{ item }}</a>
{% endfor %}

Authors

Library is by Erik Telepovsky from Pragmatic Mates. See our other libraries.

django-flatpages-i18n's People

Contributors

afrosimon avatar boekkooi avatar eriktelepovsky avatar gpichot avatar haylmfao avatar jokuf avatar merwok avatar michalochman avatar ml-chen avatar rsoltys avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

django-flatpages-i18n's Issues

"No module named xheaders" when we add the middleware and try to view a page under django 1.6.x

Hi there,

I'm getting the following error under django 1.6.x:

[[email protected]] out: Traceback (most recent call last):
[[email protected]] out:   File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
[[email protected]] out:     self.result = application(self.environ, self.start_response)
[[email protected]] out:   File "/project_share/env/local/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 67, in __call__
[[email protected]] out:     return self.application(environ, start_response)
[[email protected]] out:   File "/project_share/env/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 187, in __call__
[[email protected]] out:     self.load_middleware()
[[email protected]] out:   File "/project_share/env/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware
[[email protected]] out:     mw_class = import_by_path(middleware_path)
[[email protected]] out:   File "/project_share/env/local/lib/python2.7/site-packages/django/utils/module_loading.py", line 26, in import_by_path
[[email protected]] out:     sys.exc_info()[2])
[[email protected]] out:   File "/project_share/env/local/lib/python2.7/site-packages/django/utils/module_loading.py", line 21, in import_by_path
[[email protected]] out:     module = import_module(module_path)
[[email protected]] out:   File "/project_share/env/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
[[email protected]] out:     __import__(name)
[[email protected]] out:   File "/project_share/env/local/lib/python2.7/site-packages/flatpages_i18n/middleware.py", line 4, in <module>
[[email protected]] out:     from flatpages_i18n.views import flatpage
[[email protected]] out:   File "/project_share/env/local/lib/python2.7/site-packages/flatpages_i18n/views.py", line 2, in <module>
[[email protected]] out:     from django.core.xheaders import populate_xheaders
[[email protected]] out: ImproperlyConfigured: Error importing module flatpages_i18n.middleware: "No module named xheaders"
[[email protected]] out: [28/Feb/2014 09:08:18] "GET / HTTP/1.1" 500 59
[[email protected]] out: 

The culprit seems to be on line 3 in the views.py file.

The populate_xheaders function doesn't seem to exists anymore in the 1.6.x versions.

Here's what I did to diagnose that problem.

$ mkdir flatpages_i18n_test
$ cd flatpages_i18n_test
$ virtualenv venv
$ source venv/bin/activate
$ pip install django==1.5.4
$ grep -r --include "*.py" xheaders lib/python2.7/site-packages/django
lib/python2.7/site-packages/django/contrib/flatpages/views.py:from django.core.xheaders import populate_xheaders
lib/python2.7/site-packages/django/contrib/flatpages/views.py:    populate_xheaders(request, response, FlatPage, f.id)
lib/python2.7/site-packages/django/core/xheaders.py:def populate_xheaders(request, response, model, object_id):
## 1.6 switch
$ pip install django==1.6
$ grep -r --include "*.py" xheaders lib/python2.7/site-packages/django
$ 

How flatpages can be hierarchical?

Hi.

I'm looking for flatpages with i18n support, and I've found this project. And I'm curious why it is called "flatpages", because I see dependency on MPTT and I see parent-child relationship added. Flatpages are flat by design, not hierarchical. Have you considered a redesign?

Django 1.7 compatibility

The current "django-flatpages-i18n" doesn't work with the latest Django version (1.7), probably because of its dependence on "South". Have you planned adding compatibility with the latest version of Django?

Regards,
Luis

django-flatpages-i18n and change language form issue

I'm using django-modeltranslation in my project. Translation with my models working perfect with this form:

<form action="/i18n/setlang/" method="post">
    {% csrf_token %}
    <input name="next" type="hidden" value="{{ redirect_to }}" />
    <select name="language">
       {% get_language_info_list for LANGUAGES as languages %}
       {% for language in languages %}
            <option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected="selected"{% endif %}>
                 {{ language.name_local }} ({{ language.code }})
            </option>
       {% endfor %}
    </select>
    <input type="submit" value="Go" />
</form>

But with django-flatpages-i18n language code wouldn't change (translation not working). How may I use switch language in django-flatpages-i18n?

language code and local code

Hello, would like to let you know that I have encountered a problem with views.flatpage. Some language codes has a hyphen in it, for which, the corresponding local codes use an underscore instead. e.g. for traditional chinese, language code sent by the browser in the request object is "zh-tw". The column that has been created in the flatpage_i18n table is "zh_tw". Calling the flatpage view after setting the language to zh-tw will result in the following error

FieldError at /about/
Cannot resolve keyword 'url_zh-tw' into field. Choices are: content, content_en, content_fr, content_zh_tw, enable_comments, id, registration_required, sites, template_name, title, title_en, title_fr, title_zh_tw, url, url_en, url_fr, url_zh_tw, weight
Request Method: GET
Request URL:    http://127.0.0.1:8000/about/
Django Version: 1.5.1
...

For now, to work around it, I added the last line below to blindly replace any '-' with '_', assuming we can't have a hyphen in column names in databases, this works for my project for now.

language = request.LANGUAGE_CODE
language_prefix = '/%s' % language
language = language.replace('-', '_')

Going by the same logic, I suppose the same problem could potentially happen to the spanish and portuguese folks. (e.g. pt-BR for brazilian portuguese)

If you have other solutions, please kindly let me know!

Lastly, thanks for this wonderful package. It's exactly what I have been looking for and provides a simple-to-set-up solution for multilingual flatpages.

Regards,

Albert

not work with CKEDITOR (django-ckeditor)

When I try to use Ckeditor field widget, like this:

from django import forms
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from django.conf import settings

from flatpages_i18n.models import FlatPage_i18n
from flatpages_i18n.admin import FlatPageAdmin as BaseFlatPageAdmin
from flatpages_i18n.forms import FlatpageForm as BaseFlatpageForm

from ckeditor.widgets import CKEditorWidget

class FlatpageForm(BaseFlatpageForm):

    for lang in settings.LANGUAGES:
        field_id = 'content_%s' % lang[0]
        locals()[field_id] = forms.CharField(widget=CKEditorWidget(), \
            required=False, label=_(u'Content %s' % lang[0]))

    class Meta:
        model = FlatPage_i18n

class FlatPageAdmin(BaseFlatPageAdmin): 
    form = FlatpageForm


admin.site.unregister(FlatPage_i18n)
admin.site.register(FlatPage_i18n, FlatPageAdmin)

The editor is correctly display on the admin view, but when you try to save the form, this fail and return the error "Please correct the error below", but not field of the form is highlighted with an error.

Python 3 compatibility

A quick check shows that implicit relative imports (from models import Page instead of .models or flatpages_i18n) in a few modules prevent working with Python 3.

I could make a PR but I haven’t seen how to run tests.

Django 2.2 compatibility

I just installed django-flatpages-i18n for a Django 2.2 project. I followed the documentation through the "Migrate your database step", but after running python manage.py migrate, I got the following traceback:

  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\micha\AppData\Local\Programs\Python\Python37-32\lib\
site-packages\django\core\management\__init__.py", line 381, in execu
te_from_command_line
    utility.execute()
  File "C:\Users\micha\AppData\Local\Programs\Python\Python37-32\lib\
site-packages\django\core\management\__init__.py", line 357, in execu
te
    django.setup()
  File "C:\Users\micha\AppData\Local\Programs\Python\Python37-32\lib\
site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\micha\AppData\Local\Programs\Python\Python37-32\lib\
site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "C:\Users\micha\AppData\Local\Programs\Python\Python37-32\lib\
site-packages\django\apps\config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\micha\AppData\Local\Programs\Python\Python37-32\lib\
importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_u
nlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_mo
dule
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frame
s_removed
  File "C:\Users\micha\AppData\Local\Programs\Python\Python37-32\lib\
site-packages\flatpages_i18n\models.py", line 11, in <module>
    class FlatPage_i18n(MPTTModel):
  File "C:\Users\micha\AppData\Local\Programs\Python\Python37-32\lib\
site-packages\flatpages_i18n\models.py", line 15, in FlatPage_i18n
    null=True, blank=True)
TypeError: __init__() missing 1 required positional argument: 'on_del
ete'

Adding a bunch of , on_delete=models.CASCADE to the ForeignKeys in flatpages_i18n\models.py resolved this issue.

Does not work with Django 1.10.6

After following the install instructions I get the the following error when trying to migrate or run any manage.py command:

File "/Users/athom09/Projects/voyages/voyagesEnv/lib/python2.7/site-packages/flatpages_i18n/models.py", line 1, in <module> from builtins import str as text ImportError: No module named builtins

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.