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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:48:49+00:00 2026-05-31T00:48:49+00:00

I’m very new to web development and I know this might be stupid approach

  • 0

I’m very new to web development and I know this might be stupid approach but cannot think of a better way of doing this. In the views.py a method returns a dictionary named sniffer_aps to the template in following format:

sniffer_aps = {device1:[ap1, ap2], device2:[ap3, ap4, ap5], ......}

In the template I have a dropdown list that lists all devices(device1, device2, ……).

In another field I list all aps belong to the device selected in the dropdown list. I want to dynamically adjust the field according to the device I selected, but it doesn’t really work.

The dropdown list code is:

<select onchange="refresh(this.value)" id="sniffer_list">
            {% for sniffer_ap in sniffer_aps %}
                <option value={{ sniffer_ap }}>{{ sniffer_ap.plug_ip }}</option>
            {% endfor %}
</select>

My question is that how can I loop though the value in the field that shows aps based on the option selected in the dropdown list?

Right now my approach is to use javascript to detect the “onchange” event of the dropdown list, and change the value the field that list all aps accordingly, then just get the value of the field and treat it as the key of the dictionary.

function refresh(key) {
        $('.router').attr('value') = key;
}

For the field that lists aps, it is where the problem is. I’m trying to achieve something like this:

<select multiple="multiple" size="6">
                {% for router in sniffer_aps[this.value] %}
                    <option class="router" value="">{{ router }}</option>
                {% endfor %}
</select>

Please correct my approach, or if somebody can provide a better way. Thanks very much.

  • 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-31T00:48:50+00:00Added an answer on May 31, 2026 at 12:48 am

    You’re running into trouble here because you’re confusing server-side code (Django’s templating engine) with client-side code (the Javascript code you’re using for the onchange handler). Your server-side code will have run on the server before the resulting HTML is ever delivered to the client, so you can’t have any interactions between client-side Javascript and the template logic.

    You’re right to use Javascript and the onchange event to update the values in the field that lists apps – you could do this on the server, by submitting the form and changing the HTML with Django when the new request is returned, but this takes a new webpage load and isn’t nearly as fast or user-friendly as doing it with Javascript.

    I would approach this by:

    1. Converting the dictionary to JSON in your Django view class:

      import json
      ...
      sniffer_app_json = json.dumps(sniffer_apps)
      
    2. Outputing the JSON as a JavaScript object in your template code:

      var snifferApps = {{ sniffer_apps_json }};
      
    3. Using JavaScript to update the apps field, using this data. There are many questions addressing this on StackOverflow – you might consider using jQuery, as it makes this sort of manipulation much easier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.