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 9028615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:01:45+00:00 2026-06-16T07:01:45+00:00

Given a Django template variable with a many-to-many model, is it possible to pass

  • 0

Given a Django template variable with a many-to-many model, is it possible to pass that to a Javascript function, and access it?

(What I’m trying to get, is a list of the groups, including pk, that the current user belongs to).

For example, if I have a user jsmith that belongs to two groups, alpha and beta:

<html>
<script type="text/javascript">
    mangle_data("{{ user.name }}", "{{ user.groups }}", "{{ user.groups.all }"");
</script>
</html>

function mangle_data(username, groups, all) {
    alert("username = " + username); // works. output = "jsmith"
    alert("user.groups = " + groups); // output = "django.db.models.fields.related.ManyRelatedManager object"
    alert("all = " + all); // output = "[Group alpha; Group beta;]"
}

In all cases, the parameter passed to Javascript, is a single flattened string. Ideally, I’d get an object, or even a list that contained both group names, and group pk.

Granted, I could find the pk of a group, given its name – but that’s a lot more steps. If I can get the data from the Django template directly into Javascript, it would be much cleaner.

  • 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-06-16T07:01:46+00:00Added an answer on June 16, 2026 at 7:01 am

    How I wound up solving this, was to use a Context Processor to format the data, and pass it to a template variable:

    context_processors.py
    
    def site_settings(request):
        if request.user.is_authenticated():
            user_groups = "+".join(['{"id":%s, "name":"%s"}' % 
                (grp['id'], grp['name']) for grp in rquest.user.groups.values()])
        else:
            user_groups = ''
        return {'USER_GROUPS': user_groups, }
    
    template.html
    
    <html>
    <script type="text/javascript">
        mangle_data("{{ USER_GROUPS|safe }"");
    </script>
    </html>
    
    handle_data.js
    
    function mangle_data(groups) {
        var groups_json = '[' + groups.split('+').join(',') + ']';
        var groups_OBJ = JSON.parse(groups_json); // this is now a Javascript object.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to create a django template that can be used (nested) inside
Given a Django model likeso: from django.db import models class MyModel(models.Model): textfield = models.TextField()
Given that django-nonrel has got JOINs working, does this mean we have M2M fields
I have a django application using multiple databases. Given an instance of a model,
OK I give up - after 5 solid hours trying to get a django
I'm recently trying to get back into Django, but am having some issues trying
I have a django template which is used from many views. The template has
In a Django template, I have a function function show_graph(i){ $(#+i+seegraph).click(function(){ $(#+i+graph).show(); var qaa
I've got a Django template that initiates the Facebook OAuth process by doing: window.location='https://www.facebook.com/dialog/oauth?client_id=MY_CLIENT_ID&redirect_uri=http%3A//localhost%3A8000/fbpanel/explore-python'
How can I programmatically change sections of a Django template on the fly? Given

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.