A problem I am running into right now in my Django project is that I want to keep a large object in context between multiple pages, so that I do not have to recreate the object every time I access a new page.
My HTML design is that I have buttons that allow me to go between pages using href, but this does not allow me to create a context that allows me to transfer the object to the new page.
I do not want to store the object in the database, since this is something user-specific, and would not be helpful to other users using the website.
Is there some way to make a global variable in the view.py Django file, or some way to use href to transfer context objects, or something else that would allow me to achieve this? Thanks for your help.
You could use django session variables with redis. It's an in-memory storage that flexible enough to store most data structure you created. For django project checkout django-redis.
Related
I'm new to django and still trying to figure out logic. I watched a bunch of videos and I know how to create those examples but I'm not quite there. I'll explain what I want and maybe someone can explain to me a logic how to do, yes?
I created an app and inside an app I connected urls, settings, html and I have working site. Now I want to input data in django website, django to pass data to xlsxwriter script to create xlsx document, and to pass that document to user to download.
My logic: What I need now, is to create some kind forms (but not for registered users, it can use anyone who uses a website) to input data. Which forms are best? Than I need to pass () that data to function that are in views that will get an arguments from html page through some kind of forms, create document, and pass that to download button.
I want to dynamically create multiple dropbox fields as I input the number of fields to be created.
For instance, there are 2 fields:
integer input field
dropdown menu
so when I input(let's say "4" in field 1) then 4 dropdowns should be created.
How should it be done? And upon submitting the form the same will be stored as a list in a database (flask is used at the backend).
Please guide.
Thanks in advance.
Flask is rather lightweight framework so to create fields dynamically use Javascript. Note nowadays its preferred to develop websites using some advanced JS library or 'framework' JQuery, vue, react etc. On backend you create a controller to save the data. Start with going though a tutorial to understand the procedure. https://www.youtube.com/watch?v=6N4OmsfwfdU . If you use WTF find a tutorial or discussion specific to WTF.
Some variables (class object) needs to be shared among different view functions. Each view function changes this object's properties. This object is user-specific, that is to say, user needs to log in the web page, then access different webpage. I do not want different users share/change this object variable. What I can think of is:
Using global to declare a variable in different view functions. However, in it is running in Django runserver with multi-threaded, when multiple users access it, will the global variables be changed/shared by all users? or this variable is specific to each user?
Using session, however, the variable is object, which is not json serializable.
Store it in database, however, this is object and it is not allowed to store in the database. I cannot pickle it either.
What is the correct way of sharing variables among different view functions while allowing multiple users access server concurrently?
Thanks,
This is the problem I met. Please just explain a little bit, please do not just down vote without any comment, thank you
You say the object is user-specific. Is it also session-specific? If the user were to open a new tab in his browser and access another page in your app, could he have two different objects? If so, then it makes sense to store it in the session. You can use pickle serialization to get around JSON (although it can lead to insecurities, if the object is user-provided.)
I would like to view some user information on over half of my website's views.
This information should contain not only trivial username but also some fields from other tables of my project that are associated with current user.
I would also like to put this information into the template that my current view extends, just to keep it DRY.
I already did some research and coded some templatetags hoping that registering tags would help me achieve this but I have no idea how to get user information when there's no request like in views' functions.
Any tips on how to achieve this will be much appreciated. I just started django yesterday and am still a bit confused by it's philosophy.
You can use a context processor to add data to the template context in a DRY way.
In a nutshell, a context processor is simply a function that accepts a request as its first argument, does some additional processing that you add and augments the context with whatever values you want.
You can query an objects models, add the current datetime...pretty much anything you can do with Python or Django can go into a context processor.
I've been working with django over past couple of months. It seems that if I need some new value in a template, the only way to accomplish that is to pass it through the view function directly or an object that can be somehow used to retrieve that information.
What if I want to "PULL" information from the template? Consider the following scenario. I have a template "template1" associated with Application1. Suppose in one column of this template, I want to import information from second application "Application2". Currently the only way I know is to use the Application1's view functions to pull that info from Application2. For every new application I need to change my Application1's view function. So in case I want to keep adding information from different applications(2,3,4 etc), I would need to keep on changing the view function of Application1. This could get cumbersome.
So what I want is something like Wordpress's widget function or Joomla's module type functionality. Simple plug and play, that can "pull" the information from different sources(apps). Does django have something of this sort built-in?
Ah, you have encountered one of the fundamental differences between Django (and most Python templating frameworks) and WordPress (and most PHP frameworks). PHP has a huge global name space that can be accessed from pretty much any place in the page creation process. Python, on the other hand, does not. Many of us consider this to be a Good ThingĀ®.
However, there are times when you wish you had a few more globals for use in your templates. To accomplish this, what you want is a context processor. This is a routine that returns a dict that is automatically included whenever you use RequestContext() to build your call to your template.
You may also want to look at {% expr ... %}. It allows you to break out of the "chains" of the deliberately weak Django templating engine.