Skip to content

Instantly share code, notes, and snippets.

View bebosudo's full-sized avatar
💾

Alberto Chiusole bebosudo

💾
View GitHub Profile
@bebosudo
bebosudo / django_signals_setup.md
Created September 20, 2018 22:40
Setup signals in django

Inside your application directory, create a file your_app/signals.py.

The default project and app creator should have created a file your_app/apps.py. Inside the class used for your app, add the ready method:

class YourAppConfig(AppConfig):
    name = 'your_app'

 def ready(self):
@bebosudo
bebosudo / django_custom_widget.md
Last active January 25, 2024 02:48
Custom django form widget with no pain.

First of all, edit your_app_settings/settings.py and add:

INSTALLED_APPS = [
    'django.forms',  # to let django discover the built-in widgets
    ...
]

FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'
@bebosudo
bebosudo / gist:0c91f943013fa3c2dab86290d62de7b5
Created September 15, 2018 18:27
Django doesn't provide id in forms or serializers.
Manually provide the id with:
# forms.py
class SomethingForm(...):
id = forms.IntegerField(min_value=0)
# serializers.py
class SetIDFieldFromModel:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@bebosudo
bebosudo / django_csv_row_iterator.py
Last active September 1, 2018 17:33
Example of how to iterate over an uploaded file in django (2.1) using generic base classes.
# --- forms.py ---
class CSVImportForm(forms.Form):
file = forms.FileField()
comment = forms.CharField(max_length=50, required=False)
# --- utils.py ---
@bebosudo
bebosudo / django_widget_html5_date.py
Created August 20, 2018 19:22
A HTML5 date django widget to apply to date form fields.
from django import forms
# https://github.com/django/django/blob/master/django/forms/widgets.py
class HTML5DateWidget(forms.widgets.Input):
input_type = 'date'
template_name = 'django/forms/widgets/date.html'
# Example:
@bebosudo
bebosudo / gist:7b1c0650e2f94d26afe830530764de65
Created April 15, 2018 14:57
Compile python 3.6 locally, with openssl and sqlite3 support
Create a dir to build things into:
$ mkdir ~/stack && cd ~/stack
Download the sources (sqlite 3230100 was broken, 3220000 was used instead):
$ wget https://www.openssl.org/source/openssl-1.1.0h.tar.gz
$ wget https://sqlite.org/2018/sqlite-src-3220000.zip
$ wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz
@bebosudo
bebosudo / virtualenvwrapper_setup.md
Last active November 20, 2018 11:25
Setup virtualenvwrapper on Ubuntu 16.04 or Fedora 25+

Setup virtualenvwrapper on Ubuntu 16.04 or Fedora 25+

Install virtualenvwrapper, which eases the handling of Python virtualenvs (sort of Virtual Machines for Python packages).

$ pip3 install --user virtualenvwrapper

Add to your ~/.bashrc, or better, to your ~/.bash_profile:

@bebosudo
bebosudo / allocation_examples.c
Last active October 9, 2017 21:46
Allocations example and differences between static and dynamic allocation (in stack and heap). Italian.
/*
Da compilare su linux con:
gcc -o allocation_examples allocation_examples.c
oppure se si vuole usare il vecchio standard del C dell'89:
gcc --std=c89 -o allocation_examples allocation_examples.c
Si puo' poi eseguire facendo:
./allocation_examples
*/