Why don't we see methods within methods very often?
Like this:
def throttle_validate(self, limit, cost=0, penalty=0):
def _throttle(key):
#stuff
_throttle(self.request.ip)
if self.request.user:
_throttle(self.request.user)
As opposed to this:
def throttle_validate(self, limit, cost=0, penalty=0):
self._throttle(self.request.ip, limit, cost, penalty)
if self.request.user:
self._throttle(self.request.user, limit, cost, penalty)
def _throttle(self, key, limit, cost=0, penalty=0):
#stuff
The alternative would be to use `*args, **kwargs`, but why don't we see anyone use the first method in python?
/r/Python
https://redd.it/73yt8t
Like this:
def throttle_validate(self, limit, cost=0, penalty=0):
def _throttle(key):
#stuff
_throttle(self.request.ip)
if self.request.user:
_throttle(self.request.user)
As opposed to this:
def throttle_validate(self, limit, cost=0, penalty=0):
self._throttle(self.request.ip, limit, cost, penalty)
if self.request.user:
self._throttle(self.request.user, limit, cost, penalty)
def _throttle(self, key, limit, cost=0, penalty=0):
#stuff
The alternative would be to use `*args, **kwargs`, but why don't we see anyone use the first method in python?
/r/Python
https://redd.it/73yt8t
reddit
Why don't we see methods within methods very often? • r/Python
Like this: def throttle_validate(self, limit, cost=0, penalty=0): def _throttle(key): #stuff ...
Dynamic variable value on HTML with Django
I'm looking to display a constantly-changing python variable (that's read from a websocket server) onto a HTML file, currently i'm using a Django tag as it follows:
**templatetags/mytag.py**
from django import template
register = template.Library()
current_value = 0
@register.filter
def c_value(placeholder):
return current_value
#more code that modifies current_value reading from a websocket server
**index.html**
{% load mytag %}
<script>
function mytimer() {
setInterval(function(){
$('#stuff').html( {{ placeholder|c_value }} );
}
, 1000);
}
mytimer();
</script>
#some code from the website
<span id="stuff">Holder</span>
However naturally '{{ placeholder|c_value }}' only outputs the first ever value of 'current_value', which is 0 in this case, and so the source code of index.html ends up being:
**source code after '{{ placeholder|c_value }}'**
<script>
function mytimer() {
setInterval(function(){
$('#stuff').html( 0 );
}
, 1000);
}
mytimer();
</script>
Which is not desired since we would like to print the changing-value of 'current_value' each second.
What is the normal approach for these kind of dynamic texts? Many thanks!
/r/django
https://redd.it/82l23c
I'm looking to display a constantly-changing python variable (that's read from a websocket server) onto a HTML file, currently i'm using a Django tag as it follows:
**templatetags/mytag.py**
from django import template
register = template.Library()
current_value = 0
@register.filter
def c_value(placeholder):
return current_value
#more code that modifies current_value reading from a websocket server
**index.html**
{% load mytag %}
<script>
function mytimer() {
setInterval(function(){
$('#stuff').html( {{ placeholder|c_value }} );
}
, 1000);
}
mytimer();
</script>
#some code from the website
<span id="stuff">Holder</span>
However naturally '{{ placeholder|c_value }}' only outputs the first ever value of 'current_value', which is 0 in this case, and so the source code of index.html ends up being:
**source code after '{{ placeholder|c_value }}'**
<script>
function mytimer() {
setInterval(function(){
$('#stuff').html( 0 );
}
, 1000);
}
mytimer();
</script>
Which is not desired since we would like to print the changing-value of 'current_value' each second.
What is the normal approach for these kind of dynamic texts? Many thanks!
/r/django
https://redd.it/82l23c
reddit
Dynamic variable value on HTML with Django • r/django
I'm looking to display a constantly-changing python variable (that's read from a websocket server) onto a HTML file, currently i'm using a Django...
Why doesn't this custom mixin work?
def CustomLoginRequiredMixin(LoginRequiredMixin):
def get_permission_denied_message(self):
self.permission_denied_message='You need to be logged in to access that page'
return self.permission_denied_message
I'm calling it like this `SomeView(CustomLoginRequiredMixin, FormView): #stuff`
I'm writing a custom mixin extending the LoginRequiredMixin so that it also raises a message to display as html. However I'm getting a `TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases`.
Can anyone help me out, please?
/r/django
https://redd.it/h9lnq0
def CustomLoginRequiredMixin(LoginRequiredMixin):
def get_permission_denied_message(self):
self.permission_denied_message='You need to be logged in to access that page'
return self.permission_denied_message
I'm calling it like this `SomeView(CustomLoginRequiredMixin, FormView): #stuff`
I'm writing a custom mixin extending the LoginRequiredMixin so that it also raises a message to display as html. However I'm getting a `TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases`.
Can anyone help me out, please?
/r/django
https://redd.it/h9lnq0
reddit
Why doesn't this custom mixin work?
def CustomLoginRequiredMixin(LoginRequiredMixin): def get_permission_denied_message(self): ...