0

Consider the Models class definition: In models.py,

class Classroom(models.Model):
  subject1 = models.CharField(maX_length=80)
  subject2 = models.CharField(maX_length=80)


class Student(models.Model):
  name = models.CharField(maX_length=80)
  classroom = models.ForeignKey(Classroom) 

I am inheriting django's admin change_form.html template for the Student model. I need to access subject1 and subject2 attributes which I can do with {{ adminform.form.instance.classroom.subject1 }} and the same with subject2. But how do I access these values in the javascript of this form? Based on the value I want to hide/show some value. I don't want to use a custom view/template for this, where I could pass context variables. Can this be achieved?

user956424
  • 1,611
  • 2
  • 37
  • 67
  • 1
    If you are simply looking for access to django values within your Javascript, have a look at [this answer](http://stackoverflow.com/a/298793/233057) to a similar question. – Vernon Jan 13 '12 at 09:43

1 Answers1

0

your template can contain javascript code as well as html. just set some (js) variable to the value of some template variable

<script...>
    myJsVar = "{{ myDjangoVar }}"
</script>
second
  • 28,029
  • 7
  • 75
  • 76