Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal links from templates #55

Open
danialt opened this issue Jan 26, 2015 · 5 comments
Open

Internal links from templates #55

danialt opened this issue Jan 26, 2015 · 5 comments

Comments

@danialt
Copy link

danialt commented Jan 26, 2015

I have looked around to find out how to link directly to a fluent_page. Say I have 1 page with 2 translations:

  1. About Company, Slug: about-company, layout: about_company.html
  2. Über Firma, Slug: ueber-firma, layout: about_company.html

How can I link to the root of these pages that when say /de/ is activated it goes to the German version of the content and not the english one?

@vinnyrose
Copy link
Contributor

You should review the django-parler app.

http://django-parler.readthedocs.org/en/latest/
https://github.com/edoburu/django-parler

I'm not sure I understand exactly what you asking but I think this close to what you need:

{% load parler_tags %}
{% get_translated_url page.language_code about_page as about_page_tr_url %}

Where page is the current page. And about_page_tr_url will be the link to the page with the current language code.

@danialt
Copy link
Author

danialt commented Jan 26, 2015

@vinnyrose
Thank you for your input. Sorry if I was not clear. Your solution works indeed for the current page.

However, I'd like to link to a fluent_page node from another app/template. Say I am in books app and I want to put a link to "about us" page. How can I do that?

Thank you in advance.

@vinnyrose
Copy link
Contributor

If you're looking to render a menu I would look at some template tags:
http://django-fluent-pages.readthedocs.org/en/latest/templatetags.html#the-menu
or
http://django-mptt.github.io/django-mptt/templates.html

If you just want to link to the about page then you can get the about page in your view by either by get_for_path() (http://django-fluent-pages.readthedocs.org/en/latest/api/models.html#fluent_pages.models.UrlNodeManager.get_for_path) or use FLUENT_PAGES_KEY_CHOICES (http://django-fluent-pages.readthedocs.org/en/latest/configuration.html#fluent-pages-key-choices)

@danialt
Copy link
Author

danialt commented Jan 28, 2015

@vinnyrose Thank you very much. I think you are on the right tangant.

from fluent_pages.models import UrlNode
from fluent_pages.models.navigation import PageNavigationNode
from fluent_pages.models.utils import prefill_parent_site

UrlNode.objects.get_for_path('/uber-firma/').get_absolute_url()

gives me this error:

Traceback (most recent call last):
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2883, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-3-0c796dac7705>", line 1, in <module>
    UrlNode.objects.get_for_path('/uber-firma/').get_absolute_url()
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/fluent_pages/models/db.py", line 147, in get_absolute_url
    return self.default_url
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/fluent_pages/models/db.py", line 163, in default_url
    root = reverse('fluent-page').rstrip('/')
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/django/core/urlresolvers.py", line 551, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/django/core/urlresolvers.py", line 415, in _reverse_with_prefix
    self._populate()
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/django/core/urlresolvers.py", line 269, in _populate
    for pattern in reversed(self.url_patterns):
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/django/core/urlresolvers.py", line 372, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/django/core/urlresolvers.py", line 366, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/Users/danialt/Projects/firma_pydev/firma_pydev/urls.py", line 16, in <module>
    url(r'^admin/', include(admin.site.urls)),
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 262, in urls
    return self.get_urls(), self.app_name, self.name
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 246, in get_urls
    url(r'^%s/%s/' % (model._meta.app_label, model._meta.model_name), include(model_admin.urls))
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/django/contrib/admin/options.py", line 598, in urls
    return self.get_urls()
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/parler/admin.py", line 264, in get_urls
    urlpatterns = super(TranslatableAdmin, self).get_urls()
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/polymorphic_tree/admin/parentadmin.py", line 152, in get_urls
    base_urls = super(PolymorphicMPTTParentModelAdmin, self).get_urls()
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/polymorphic/admin.py", line 295, in get_urls
    self._lazy_setup()
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/polymorphic/admin.py", line 134, in _lazy_setup
    child_models = self.get_child_models()
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/fluent_pages/adminui/urlnodeparentadmin.py", line 73, in get_child_models
    for plugin in page_type_pool.get_plugins():
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/fluent_pages/extensions/pagetypepool.py", line 89, in get_plugins
    self._import_plugins()
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/fluent_pages/extensions/pagetypepool.py", line 210, in _import_plugins
    import_apps_submodule("page_type_plugins")
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/fluent_utils/load.py", line 54, in import_apps_submodule
    if _import_module('{0}.{1}'.format(app, submodule)) is not None:
  File "/Users/danialt/anaconda/envs/pycare_min/lib/python2.7/site-packages/fluent_utils/load.py", line 67, in _import_module
    return __import__(module_label, fromlist=classnames)
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
**ImportError: No module named page_type_plugins**

I am trying to get the node for "uber-firma", which is "de" default language, and then display "about-company" link in the template. Is there any easier way to do this, e.g. a template tag?

Many thanks!

@vdboor
Copy link
Contributor

vdboor commented Feb 23, 2015

If you have some special page where you want to link to, you can also use Page.objects.get_for_key("...") function.

If you define FLUENT_PAGES_KEY_CHOICES in your settings file, you can select a "page identifier" in the admin (hidden under the "Publication preferences").

I use this to link to a "Terms and conditions" page or contact page.

For regular content, I'd use a menu as @vinnyrose suggested.

The exception you get happens because PyDev hooks the import code. You can fix this by running pip install django-fluent-utils>=1.1.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants