Tagged: snippet Toggle Comment Threads | Keyboard Shortcuts

  • penguin 11:06 on 2018-10-17 Permalink | Reply
    Tags: , , , snippet   

    Misc Django I – forms 

    Custom form errors

    If you want to validate something in the view, and return with a custom error message in the same form, you can use the “Form.add_error(fieldname, errorstring)” method. And then, of course, return to the previous template.

    class MyView(View):
        def get(self, request):
            data = form.cleaned_data
            if len(res) > 0:
                form.add_error(
                    'login', 
                    "Diese Personalnummer existiert bereits!")
                return render(
                    request, 
                    'my_template.html',
                    {'form': form})

    Dynamic choice fields in forms

    You want a form which fills its choice field from the database? And if the database changes, if you reload the page, the form should change as well? Of course! Django got you covered.

    class UserForm(forms.Form):
    
        def __init__(self, *args, **kwargs):
            super(UserForm, self).__init__(*args, **kwargs)
            self.fields['site'] = forms.ModelChoiceField(
                label="Site",
                queryset=Site.objects.all().order_by('name'),
            )
            for field in ('department', 'office', 'phone'):
                self.fields.move_to_end(field)
    
        login = forms.CharField(label="Login")
        email = forms.EmailField(label="Email")
        site = None             # this is set in __init__() :)
        department = forms.CharField(label="Department")
        office = forms.CharField(label="Office")
        phone = forms.CharField(label="Phone")

    … now, why the “for field in (‘department’ …)” line you ask?

    Simple. The fields dict is an OrderedDict. If you replace a field it is appended to the end again. So in the form the “Site” input box would be displayed last, although it makes more sense to display it where it is in the original definition.

    Using “.move_to_end()” you can re-adjust this. If someone knows a better method … feel free to tell me.

    (Sources: here)

     
  • penguin 18:59 on 2018-09-26 Permalink | Reply
    Tags: , snippet   

    Firefox close tab buttons on mouse hover 

    I used to have Firefox’s TabMix Plus addon installed. And the feature I missed most – surprisingly – is to have the “close tab” buttons appear on a tab when you hover the mouse over it.

    Googling a little bit told me how to bring it back:

    .tabbrowser-tab:not([selected]):not([pinned]) .tab-close-button {
      visibility: hidden !important;
      margin-left: -16px !important;
    }
    .tabbrowser-tab:not([selected]):not([pinned]):hover .tab-close-button { visibility: visible !important; margin-left: 0px !important; display: -moz-box !important; }
     
  • penguin 16:55 on 2016-07-06 Permalink | Reply
    Tags: snippet   

    Quick puppet debugging snipper for Atom 

    Not sure how I could have lived without this until now (had it before in Sublime, never bothered porting, stooooopid as I realize now 😉 ):

     
  • penguin 16:22 on 2016-06-28 Permalink | Reply
    Tags: , , snippet,   

    Testing logstash configs with Docker 

    Now this is really not rocket science, but since I might do this more often, I don’t want to google every time.

    Prepare your directories

    Prepare your logstash config

    Run logstash

    Done.

    Done. 🙂

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel