Re: Django Templates and Conditional CSS classes

2017-11-11 Thread Omar Helal
Hi treyd, 
I think idea of updating the model and storing it in the db is a bit 
overkill, however you could create an @propery on the model that will do 
that logic for you and return the css class for you.
e.g.
@property
def css_class(self):
  if self.state is SUCHANDSUCH:
 return "label-soandso"

and so in the template you can do what Vijay recommended but without 
storing anything you don't need in the database.

Good luck with going Angular, it plays well with Django! 

On Saturday, 11 November 2017 09:50:38 UTC+11, treyd wrote:
>
> Thanks, all, for the info.  Looks like the best way to clean this up is to 
> make the model have some knowledge of the CSS class I want to use and make 
> a method to get it.  I like that, but I was unsure about sticking view 
> information into the model.  Probably the best solution is javascript logic 
> in the browser, which I'll get to.
>
> On Friday, November 10, 2017 at 5:45:02 PM UTC-5, Vijay Khemlani wrote:
>>
>> You can also add a "state_css_class" (or something) method to your object 
>> class and just call
>>
>> ...
>>
>> the method should be quite simple
>>
>> On Fri, Nov 10, 2017 at 7:01 PM, Adam Simon  wrote:
>>
>>>
>>>  You can pass the class from either the model or the view into the 
>>> template. For example, you can assign a class to a button with a variable:
>>>
>>> <|button>
>>>
>>> And then the class would have to be defined in your style somewhere
>>>
>>> On Fri, Nov 10, 2017 at 1:37 PM treyd  wrote:
>>>
 At some point I plan on figuring out how to do more intelligent 
 front-end in-browser stuff (Angular, etc) which yeah would definitely help 
 me here but I was wondering if there was a Django-only way of doing this.


 On Friday, November 10, 2017 at 3:51:09 PM UTC-5, Adam wrote:

>
> Are you open to using JavaScript in the front end? It would make it 
> really easy
>
 On Fri, Nov 10, 2017 at 12:40 PM treyd  wrote:
>
 Hello,
>>
>> I am trying to render a model field in a django template, and, based 
>> on the value of the field, I want to specify a different CSS class.  
>> Right 
>> now, my solution is this:
>>
>> {{ myobject.state }}
>>
>> This works, but seems like a really ugly solution and a bunch of 
>> logic in the template.  Is there a better way of doing this?  My thought 
>> was to put the class names as another field in the model, but that seems 
>> like it would violate the MVC (or MVT) separation. Any ideas?
>>
>> Thanks in advance,
>> treyd
>>
>> -- 
>> You received this message because you are subscribed to the Google 
>> Groups "Django users" group.
>>
> To unsubscribe from this group and stop receiving emails from it, send 
>> an email to django-users...@googlegroups.com.
>> To post to this group, send email to django...@googlegroups.com.
>
>
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/6a3adb62-f3d7-4cc2-8c97-add34cf757ce%40googlegroups.com
>>  
>> 
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
> -- 
> -- 
>
> Adam F. Simon, PhD
> Calabasas, CA.
>
> cell:  818-425-3719
> home:   818-880-8405
>
> Feel free to link w/ me: LinkedIn
>
>
> -- 
 You received this message because you are subscribed to the Google 
 Groups "Django users" group.
 To unsubscribe from this group and stop receiving emails from it, send 
 an email to django-users...@googlegroups.com.
 To post to this group, send email to django...@googlegroups.com.
 Visit this group at https://groups.google.com/group/django-users.
 To view this discussion on the web visit 
 https://groups.google.com/d/msgid/django-users/360d495a-6715-40a6-ae25-4ee5aed7e3cc%40googlegroups.com
  
 
 .
 For more options, visit https://groups.google.com/d/optout.

>>> -- 
>>> -- 
>>>
>>> Adam F. Simon, PhD
>>> Calabasas, CA.
>>>
>>> cell:  818-425-3719
>>> home:   818-880-8405
>>>
>>> Feel free to link w/ me: LinkedIn
>>>
>>>
>>> -- 
>>> You received this message because you are subscribed to the Google 
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send 
>>> an email to django-users...@googlegroups.com.
>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/django

Django Signals - trigger when model changed

2019-10-05 Thread Omar Helal
Hi all,

I came across this ticket in stack overflow to try to find a solution for the 
issue of calling a signal only when a particular field on a model was updated.

https://stackoverflow.com/questions/39034798/django-signals-for-models-value-change-condition

I didn't want to use any third party libraries (especially one that had 
migrations on the DB) and wanted to use the built in signals provided by Django.

My initial solution is to:

1) Create the function I want to trigger when the particular field has changed 
(in the questioner's case the music field on the Album instance)

def do_this_when_album_music_changed(sender, **kwargs):
// do something
print("I'm only going to be called if the field has changed")

2) Create the custom signal I want to send when the particular field changes

album_music_changed = Signal(providing_args=[])

3) Create a pre_save signal receiver that will check whether or not the field 
has changed and "connect" the custom signal to the receiver, otherwise 
"disconnect" it

@receiver(signal=signal=models.signals.pre_save, sender=Album)
def album_pre_save(sender, instance, **kwargs):
old_album = sender.objects.get(pk=instance.pk)
if not old_album.music == instance.music:
album_music_changed.connect(do_this_when_album_music_changed, 
sender=Album)
else:
album_music_changed.disconnect(do_this_when_album_music_changed, 
sender=Album)

4) Finally add a post_save signal that will actually send the trigger to call 
the custom signal and hence the function you want to run

@receiver(signal=signal=models.signals.post_save, sender=Album)
def album_post_save(sender, instance, **kwargs):
album_music_changed.send(sender=sender)

In my case, I need to have the function run after post_save of the instance so 
this was the best thing I could come up with, if anyone has a better solution 
that would be awesome if you could share it.

Of course if you don't need the triggered function to run post_save and you are 
happy to run it pre_save you can just call the .send() inside the first "if" 
block and not have to worry about the post_save receiver

Omar

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/DF8CCA82-704A-4805-A16E-9D47096AA27E%40umda.net.