Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3489226
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:22:12+00:00 2026-05-18T11:22:12+00:00

My Django model has a field within which I want to have reference logic.

  • 0

My Django model has a field within which I want to have reference logic. As an example:

This is an example of text in the field.[ref type="quotation" name="Martin" date="2010"]

When presented in the final markup, this is rendered as (reduced example):

This is an example of text in the field.<a href="#ref">1</a>
[SNIP]
<ul>
<li><a name="ref1">Martin, 2010</a></li>
</ul>

So, essentially, I am building a list of references to go into a different {{}} block further down the page.

Should this kind of text-processing logic be in the view (so I pass 2 values to the template, 1 that is the modified text and 1 that is the reference table), or is there some more Django-esque way to do it via filters etc.?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:22:13+00:00Added an answer on May 18, 2026 at 11:22 am

    If you’re actually storing the references in the text field like, this, then essentially you’re using a simple markup language to store the references.

    In which case, I think the template would be the place to do this.

    Unfortunately, I don’t know of any way to have a filter create and write to a context variable. So instead of using a filter, you’re going to have to use a tag, something like:

    {% output_with_references article_content myreferencesvar %}
    
    [snip]
    
    <ul>
    {% for ref in myreferencesvar %}
    <li><a name="{{ ref.id }}">{{ ref.authors }}, {{ ref.year }}</a></li>
    {% endif %}
    </ul>
    

    BTW: if there is a way to write to the page context while using a filter, I’d love to know about it.

    Update

    To implement it, you’d use something like:

    from django.template import Library, Node, TemplateSyntaxError
    
    register = Library()
    
    class OutputWithReferencesNode(Node):
        def __init__(self, input, ref_varnam='references'):
            self.input = input
            self.ref_varnam=ref_varnam
    
        def render(self, context):
            output = self.input
            references = []
            # process self.input
            context[self.ref_varnam] = references
            return output
    
    @register.tag
    def output_with_references(parser, token):
        try:
            fnctn, input, ref_varname = token.split_contents()
        except ValueError:
            raise TemplateSyntaxError, "%s takes the syntax %s text_to_output references_variable_name" % (fnctn,)
        return OutputWithReferencesNode(input, ref_varname)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want a Django model to have a date field in which the year,
I have this model which has Image field to be uploaded. It has a
I have a django model, which has a int field (with null=True, blank=True). Now
I have a Django model which has a foreign key that is optional. I
I have a model using Django-MPTT that also has a field for using a
I have a django model which has a load of relatively small fields and
I have a Django model called Event that has a date field for the
I have a django model (A) which has a ManyToManyField (types) to another model
I have created a blog app which its model has an author field like
In django I have to models A and B, B has FK reference to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.