[Answered ]-Django: DRY Internal links in TextFields

2πŸ‘

βœ…

I just answered a similar question on SO, and it seems like it might solve your problem as well (if by chance you’re still looking for an answer three years later).

I wrote a template filter to parse the custom internal link format in the Textfield before display. I’m using Markdown to parse my textfields, so I made the links return in Markdown format, but they could easily be written as HTML instead.

Check out my answer here.

Update: I posted a revised version on djangosnippets.org that resolves internal links inside a markdown-formatted link, as well as on their own.

πŸ‘€swizzlevixen

0πŸ‘

if I got your problem, you should use a custom template processor to pass a dictionary to your templates:
in settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "myapp.myprocessor.foo",
)

in myapp/myprocessor.py:

from django import template    
def foo(request):
  ProjectA = get_Project_from_database
  t = template.Template(ProjectA.html)
  c = template.Context({'name': ProjectA.name})
  rendered_ProjectA = t.render(c)
  return { 'rendered_ProjectA': rendered_ProjectA }

or if you don’t wanna use Django template system you can use regular expressions (import re)

πŸ‘€Kambiz

Leave a comment