How hide/show a field upon selection of a radio button in django admin?

2024/11/14 20:20:11

models.py

from django.db import models
from django.contrib.auth.models import UserSTATUS_CHOICES = ((1, 'Accepted'),(0, 'Rejected'),)
class Leave(models.Model):----------------status = models.IntegerField(choices=STATUS_CHOICES, default = 0)reason_reject = models.CharField(('reason for rejection'),max_length=50, blank=True)def __str__(self):return self.name

admin.py

from django.contrib import admin
from .models import Leave@admin.register(Leave)
class LeaveAdmin(admin.ModelAdmin):-----------------class Media:js = ('/static/admin/js/admin.js')

- admin.js

    (function($) {$(function() {var reject = document.getElementById('id_status_0')var accept = document.getElementById("id_status_1")var reason_reject = document.getElementById("id_reason_reject")if (accept.checked == true){reason_reject.style.display = "none"}else{reason_reject.style.display = "block"}});
})(django.jQuery);

Now I have imported the file in the admin.py, How do I trigger the jQuery function such that it works.

update-

the function works but, I need to reload the page to make the field appear and disappear. I want some thing equivalent to 'on-click' event in HTML. I have no idea about Javascript.

Answer
$(function() {$('input[name="status"]').on('click', function() {if ($(this).val() == '0') {$('#id_reason_reject').show();}else {$('#id_reason_reject').hide();}});
})
https://en.xdnf.cn/q/71818.html

Related Q&A

format/round numerical legend label in GeoPandas

Im looking for a way to format/round the numerical legend labels in those maps produced by .plot() function in GeoPandas. For example:gdf.plot(column=pop2010, scheme=QUANTILES, k=4)This gives me a lege…

Python pickle crash when trying to return default value in __getattr__

I have a dictionary like class that I use to store some values as attributes. I recently added some logic(__getattr__) to return None if an attribute doesnt exist. As soon as I did this pickle crashe…

How to download google source code for android

As you know, there is a list of several hundred projects in https://android.googlesource.com/. Id like to download them all in windows machine. According to Googles document,To install, initialize, and…

Compute on pandas dataframe concurrently

Is it feasible to do multiple group-wise calculation in dataframe in pandas concurrently and get those results back? So, Id like to compute the following sets of dataframe and get those results one-by…

How do I go about writing a program to send and receive sms using python?

I have looked all over the net for a good library to use in sending and receiving smss using python but all in vain!Are there GSM libraries for python out there?

Persist Completed Pipeline in Luigi Visualiser

Im starting to port a nightly data pipeline from a visual ETL tool to Luigi, and I really enjoy that there is a visualiser to see the status of jobs. However, Ive noticed that a few minutes after the l…

How to assign python requests sessions for single processes in multiprocessing pool?

Considering the following code example:import multiprocessing import requestssession = requests.Session() data_to_be_processed = [...]def process(arg):# do stuff with arg and get urlresponse = session.…

Missing values in Pandas Pivot table?

I have a data set that looks like the following:student question answer number Bob How many donuts in a dozen? A 1 Sally How many donuts in a do…

Selecting Element followed by text with Selenium WebDriver

I am using Selenium WebDriver and the Python bindings to automate some monotonous WordPress tasks, and it has been pretty straightforward up until this point. I am trying to select a checkbox, but the …

AttributeError: module keras.backend has no attribute image_dim_ordering

I tried to execute some tutorial transfer learning project. But Ive got attribute error.I checked my tensorflow and keras version.tensorflow : 1.14.0 keras : 2.2.5and python 3.6.9 version.the code is h…