5

I have a python script written up that returns a list of values from a database. I want to incorporate that script into my django website that I have created. I have an html file right now in my templates folder that has dictionary values hardcoded but how do I replace the dictionary hardcoded material with the script, lets call it values.py

<script type="text/javascript">
$(document).ready(function() {
    var dropDown = [" ", "Run1", "Run2", "Trail1", "Trail2"];
    var dropDownID = [" ", "111111", "222222", "333333", "444444", "555555"];
    $("#dropDown").select2({
        data: dropDown
    });

    $("#dropDown").change(function() {
        $("#dropdownID").val(dropDownID[$("#dropDown option:selected").index()]);
    });
});

5
  • Where's your python script or values.py code ?, please post more info so we can help Commented Feb 1, 2017 at 22:19
  • @jsanchezs the python code I put into my project directory and then in a folder called "templatetags" inside is values.py and init.py Commented Feb 1, 2017 at 22:23
  • Yeah, tags are indeed the way to achieve that but post your python code in the question so we can figure out how it works and understand exactly what you need to guide you through Commented Feb 1, 2017 at 22:24
  • @jsanchezs its a rather long script and is connected to other features within the database, if you can just guide me to how to even submit a python script into django that would be appreciated. Lets just use a simple dictionary one like the hardcoded above or we can forget about dictionary and just do a simple hello world script Commented Feb 1, 2017 at 22:30
  • @VisualExtasy I think question it's quite different from the code you posted, anyhow i posted you a basic example of how tags work. remember to mark as correct the answer if it helped. Commented Feb 1, 2017 at 22:44

1 Answer 1

8

As we talked, tags are the way to achieve that....Let's say you want to do a simple "Hello world" tag, this would be your tag.py code :

tag.py

from django import template

register = template.Library()

@register.assignment_tag
def hello_world(name):
    salute = 'Hello' + name
    
    return salute

As you see, you got all the python code there, depending on what you need you may need to import models or anything else from database...assuming you got all that in order as you say, you just create the function, process all code you need and return something (dictionary of data in your case to iterate in template), but for this simple example just a string with the name.

Then, inside your template you load your tag file, and call the function sending the specific parameter to use it later as i show you here :

Template.html

{% load tag %}
<div id="id_div">
    {% hello_world 'Foo' as salute_text %}
    <strong> {{ salute_text }} </strong>
</div>

Hope it helps to clarify!!

Sign up to request clarification or add additional context in comments.

12 Comments

I get a tag.py is not a registered tag library error.
Did you tried my code from zero or are you using your file called values.py ?, remember in this example tag.py should be the name of the file and it should be in templatetags directory.
Yea I used yours tag.py in templatetags directory and then in the calling its {% load tag.py %}
Sorry, it has no '.py' at the end, i updated my answer, it's : {{ load tag }}
it works :D, but do you mind explaining it to me like what is always required when doing template tags and what is just for this example only. I'm assumming the template import and register = template.Library has to be done everytime
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.