how to trigger a django function when the user logout - django

I tried to use django signals to be triggered for five different models. But, I also have the same models being used in another function and this function imports an XML file. So, it makes my page slow because of the signal.
I was wondering if I can change it to a normal function to be triggered when the user clicks in logout. How can it be done? Should I use ajax to do it or there is another way?

Related

Why use signals in Django?

I have read lots of documentation and articles about using signals in Django, but I cannot understand the concept.
What is the purpose of using signals in Django?
How does it work?
Please explain the concept of signals and how to use it in Django code.
The Django Signals is a strategy to allow decoupled applications to get notified when certain events occur. Let’s say you want to invalidate a cached page everytime a given model instance is updated, but there are several places in your code base that this model can be updated. You can do that using signals, hooking some pieces of code to be executed everytime this specific model’s save method is trigged.
Another common use case is when you have extended the Custom Django User by using the Profile strategy through a one-to-one relationship. What we usually do is use a “signal dispatcher” to listen for the User’s post_save event to also update the Profile instance as well.

How do I have the deletion of a user in Django trigger execution of some additional code?

In my Django application, when a user is created, a database is created too. This is defined in my registration view. I have the database creation to make up for the lack of support for dynamic models. My problem is that I want to have the deletion of a user in the Django admin page call some code which will also delete the database. I am not sure how to do this.
Depending on what code you want to call, consider post_delete as an option, if it doesn't metter for you where object being deleted or overriding delete_model method, in case you want to run this code specifically during delete via admin.

Where is the best place to send email in django-rest-framework?

I want to send emails in django rest framework, for example when someone creates an account,or comments something and etc. I found this in django documents. But I don't know where to use it. maybe in Viewset but in which method.
so what is the best way and where is the best place to send emails in Django Rest Framework?
I would like to give you couple of suggestions(Of course, with reasons):
That is synchronous. Maybe that would be not desired/efficient. I would suggest you to use something like this package (django-anymail) , this will decouple your email sender. Assume now you're using AWS SES, tomorrow you want to switch to sendgrid, then you'll only need to change setting variables with this.
Assuming User, Comment each of these are separate models. I would suggest you to override the save() method of these models and send email from there. I am not suggesting signals because I feel (fully personal view) that signals are to be used when access to the source is not possible, like some event in 3rd party library, etc.
Assuming that "Creating an account" and "commenting on something" both create some model instance, you could use existing model signals.
If you need to catch events that don't already send a signal, you will have to define new signals and send them from the appropriate place (which depends on what you're interested in so no 'one-size-fits-all' answer here).
Also if you have more than one mail to send at a time you may want to consider using some async task queue to send the emails - this avoids delaying the response (django signals are NOT async).
You can register sending function to some django signals or You can call it in Class-based Views or Function Based Views.

call a helper function and create a progress bar on django

I want to make a progress bar for a poll. I use django 1.4.8 for the implementation. The progress bar should display a rercentage of people who have already vote. There is also a helper function which return the sum of users voted.
At first i edit the template and the css to display the bar. After that, i try to use jquery for the actual behavor. But I am lost. I searched on the internet but i could make understanding in a clear manner. I am not sure which is the right way to do it. I dont want to use any other external library.
So, the question is how to call the help function and where i ll use the value to display the proper percentage on the progress bar into the template?
You need to think about how the web browser and the server (Django) communicate.
The browser makes a 'get' request to a url on the server. In Django you have an urls.py file which determines which view will handle requests to that url. For example there will be a view which renders a template containing the HTML code for the poll form.
When the user submits the poll, the browser makes a 'post' request, sending the form data for the poll to the server - to a specific Django view which handles saving the poll vote to the database. Usually in Django we'd use the same view function for both the get and the post for a form - see this answer for more details: https://stackoverflow.com/a/21114637/202168
It's not clear whether you want to show the progress bar initially, or only after they've submitted their vote. Either way you just need to calculate the relevant values for percentage and sum of users in your Django view and pass those values into the template, which renders the HTML sent back to the browser.
If you're using an HTML5 progress bar you don't even need jQuery. Unless you want a user to see the progress bar changing as other users submit their votes (much more complicated) you don't need any 'ajax' stuff either.
I suggest first you start with the Django tutorial:
https://docs.djangoproject.com/en/dev/intro/tutorial01/
So to be accurate the helper.py imports register from jingo. This means you call the function instantly into template. Furthermore I used instead of for my purpose.
helper.py
from jingo import register
#register.filter
def get_value():
return value
into template
<meter min="0" value="{{ <app_name>|get_value }} max="1"></meter>

Applying Javascript MVC architecture on existing backend MVC

We currently have a large implementation done in Django which mostly spit out static HTML. We are planning to implement a javascript modular framework to hook to django's API and define needed routes..etc
But, we do not want to scratch off what we did so far as the site is completely functional for non-js users. The more I read about JavaScript MVC designs, the more I find it impossible to implement the two to work together.
Any ideas of how to proceed in that direction? is there a best practice that someone can follow?
Keep what you got right now as a fail-safe. Create new, separate views meant for Ajax requests that handle the processing of data and respond with appropriately formatted respond data (XML, JSON etc.).
Basically, Django is your model. Any action defined by your Javascript controller can either modify the model or the view.
If it modifies the view, there are two options: you either change just the display of data, and thus you just call a Javascript function that alters the HTML, or you display new data. In that case, you have to create a Django view that supplies that data in a format that Javascript can process easily. In your Ajax request, supply a callback (the view part of your Javascript) that handles displaying that data.
If the action modifies the model, you also need to create a Django view that processes the POST data sent by the Ajax request as needed, and returns either the error messages or a success message in a format that is, again, easily processed by Javascript. Again, you should register a callback to your Ajax request that handles the display of the messages returned by Django.
So basically, the only change to your current views would be that you supply the appropriate Javascript code to map the actions to Ajax requests and to handle the data returned by Django for your Ajax requests. Everything else should be done in separate Django views.