If the session is true, then create a div element

views.py

def edit_report(request, report_id):

    user = request.user
    if 'report_id' in request.session:
        del request.session['report_id']
    try:
        member = Members.objects.get(member=user)
        account_user = member.user
    except:
        account_user = user.id
    request.session['report_id'] = report_id
    request.session['account_user'] = account_user
    request.session["edit_report"] = True  
    return redirect('method_name')

When a button is clicked in my application, it leads to the edit_report method. If the session variable request.session["edit_report"] becomes true during this process (set with

request.session["edit_report"] = True
), I would like to use JavaScript to check for this condition and then display a hidden div by setting its style to "display: inline". How can I achieve this using Django?

Answer №1

If you include

django.core.context_processors.request
in the TEMPLATE_CONTEXT_PROCESSORS setting, you will gain access to the request variable within your templates.

You can then set a JavaScript variable to request.session.edit_report in your template, like so:

<script type="text/javascript> 
   var edit_report = {{ request.session.edit_report|yesno:"true,false" }};
</script>

Afterwards, you can use the edit_report variable in your JavaScript code.

For further reference, check out:

  • django request in template
  • How can I change the way a boolean prints in a django template?

I hope this information proves helpful.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating an automatic image carousel using a combination of Jquery, Javascript, and CSS

I have been working on creating a slider using jQuery and javascript, but I am facing some issues with the autoplay functionality and the next-previous buttons. This slider is intended for displaying images as a title indicates. Can anyone assist me with i ...

I encountered a RangeError with code [ERR_HTTP_INVALID_STATUS_CODE] due to an invalid status code being undefined

I have encountered a specific error while running my note-taking app. The error seems to be related to the following piece of code - app.use((err,req,res,next)=>{ res.status(err.status).json({ error : { message : err.message ...

Searching Firestore arrays for objects based on one of two fields

Within my document, I am working with the following array: https://i.sstatic.net/j9hBT.png I have a SizeFilterComponent that emits a BaseFilter object when a size is selected. Multiple sizes can be selected. Here is the method handling this logic: selecti ...

Looking for tips on programmatically adjusting the row count in a react table? Calling all React developers!

Currently, I have a table that is displaying data from an APP_DATA object. However, I am looking to add a column showing the number of rows dynamically next to each row. How can I achieve this? Keep in mind, only "prt" and "cat" are arrays, so you can onl ...

Is it possible to use Jade for server-side Node.js development?

Is there a way to include server-side Node.js code (like the example below) in a Jade file or other template engines (or without using template engines)? extends layout block content p Welcome to #{title} <!-- Here is some Node.js program that r ...

arranging select options in alphabetical order

One of the questions posed to me in an interview was about sorting a select option. The options were listed as follows: <select> <option>aaa</option> <option>ccc</option> <option>ddd</option> <option>bbb< ...

Issues with the parseInt() Function

I'm having trouble figuring out why parseInt() isn't working correctly in my code, especially when passing numbers through to my function parameters. It keeps returning NaN in both my array and the function's return value. What I'm att ...

Execute the function if the text or value begins with a certain character

I'm trying to determine whether the text within a span starts with the letter "x" and then execute a function. I've come across using "^" in strings for this purpose, but am having trouble implementing it to check the text... <span>xfoo&l ...

Concealing elements using react navigation

Just diving into React.js and I've got a question regarding react router. I'm a bit confused about nested routes in react router. Let's say we have the following code snippet (taken from react-router's github page) <Router> < ...

Exploring the functionality of trading-vue library in a simple setup using vanilla HTML and Vue CDN

<html> <head> <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7a0c0f1f3a48544c544b48">[email protected]</a>/dist/vue.js"></script> ...

Is there a more efficient method to replace the element at arr[i] without using arr.splice() that is not timing

Having trouble with my solution and suspect that the .splice() function might be in the wrong place since I keep timing out. The issue: You have an array of integers. Each move allows you to increase one element by one. Find the minimum number of moves ...

Unable to reach axios within the vuex Store

Below is the code snippet from my store.js file: import Vue from 'vue'; import Vuex from 'vuex'; import axios from 'axios/dist/axios'; import VueAxios from 'vue-axios' const api = axios.create({ baseURL: 'my ...

What could be the reason for my function failing to return true?

I have a function that can check if a script is loaded: function checkURLExistence(url){ $.ajax({ url: url, success: function(data){ //alert(url + ' exists'); console.log(url + ' exists'); return ...

Determining the Testing Status of a Node Package Management (NPM) Package

As someone who is new to Node.js and NPM, I have a question that may seem naive. Is there a method to determine if a package published on NPM has been tested? And if so, can this process be automated? Are there any tools or frameworks available that can va ...

Use vanilla JavaScript to send an AJAX request to a Django view

I'm attempting to make a GET AJAX request to a Django view using vanilla JS. Despite passing is_ajax(), I am having trouble properly retrieving the request object. Below is my JavaScript code. Whether with or without JSON.stringify(data), it does not ...

Deleting a record in MongoDB based on a specific value present in a column

I am in search of more information about delete triggers in MongoDB. Source: Query on MongoDB Delete Triggers I am interested in converting DELETE operations to UPDATE + AUTOMATIC DELETE. To achieve this, I plan to introduce a new field called "flag" ...

Detecting the State of the Keyboard in Ionic 2

Seeking an easy way to determine if the mobile device keyboard has been opened or closed using Ionic2 and Angular2. Is there a 'keyboard-open' or 'keyboard-close' class that Ionic sends to the body/html? ...

What's preventing the mobx @computed value from being used?

Issue: The computed value is not updating accordingly when the observable it is referencing goes through a change. import {observable,computed,action} from 'mobx'; export default class anObject { // THESE WRITTEN CHARACTERISTICS ARE COMPUL ...

Troubleshooting EasyTabs: Localhost Issue with Ajax Tab Configurations

I've implemented EasyTabs for organizing my tabs on a webpage. I decided to use ajax-tabs functionality in order to fetch content from other pages when users click on the navigation menu buttons. However, I've encountered an issue where the conte ...

Creating links in CodeIgniter with the 'anchor' function

I recently started working with the codeigniter framework and I need help converting this PHP code into a Codeigniter version echo '<td><a rel="facebox" href=useredit.php?emp_id='. $row['emp_id'] .'><button type="bu ...