Skip to content

Instantly share code, notes, and snippets.

@faulander
faulander / html
Last active July 17, 2021 17:54
[Call a function after htmx swap] #htmx #python #django
<script>
htmx.on("htmx:afterSwap", function(evt) {
<Function_to_call>;
});
</script>
@faulander
faulander / dateandtime.py
Last active July 17, 2021 17:48
[Function to return all dates for ALL weeks in a given month] #python
import copy
import calendar
import pendulum as p
def getMonthDates(year:int, month:int, includeWeekends:bool = False) -> dict[list[p.datetime]]:
monthdates = list()
cal = calendar.Calendar()
dates = {}
monthdates = []
for countweek, week in enumerate(cal.monthdatescalendar(year,month)):
@faulander
faulander / admin.py
Last active July 17, 2021 17:45
[Convert a foreign key field into clickable links] #python #django
def linkify(field_name):
"""
Converts a foreign key value into clickable links.
If field_name is 'parent', link text will be str(obj.parent)
Link will be admin url for the admin url for obj.parent.id:change
"""
def _linkify(obj):
linked_obj = getattr(obj, field_name)
@faulander
faulander / base.html
Last active July 17, 2021 17:49
[Include htmx in Django Webpages] #htmx #django #python
<script>
document.body.addEventListener('htmx:configRequest', (event) => {
event.detail.headers['X-CSRFToken'] = '{{ csrf_token }}';
})
</script>
@faulander
faulander / html
Last active July 17, 2021 17:53
[Django Crispy Forms Setup] #Django #Python
{% crispy form %}
<form action="{% url 'form handler url' %}" class="uniForm" method="post">
{% crispy first_form %}
{% crispy second_form %}
</form>