Skip to content

Instantly share code, notes, and snippets.

Avatar

Marcelo Leiva marceloleiva

View GitHub Profile
@marceloleiva
marceloleiva / .env-example
Created Apr 3, 2020 — forked from gjbagrowski/.env-example
Django-environ example
View .env-example
# project-repo/app/settings/.env-example
#
# DJANGO
#
SITE_HOST=localhost:8000
USE_SSL=False
ALLOWED_HOSTS=localhost,127.0.0.1
SECRET_KEY=asdasd
DEBUG=true # never on production, will cause settings including api keys to leak
DJANGO_LOG_LEVEL=DEBUG
@marceloleiva
marceloleiva / forms.py
Created Jul 7, 2019 — forked from eerien/forms.py
Comma Separated Values Form Field for Django. There are CommaSeparatedCharField, CommaSeparatedIntegerField.
View forms.py
from django import forms
from django.core import validators
from django.core.exceptions import ValidationError
class MinLengthValidator(validators.MinLengthValidator):
message = 'Ensure this value has at least %(limit_value)d elements (it has %(show_value)d).'
class MaxLengthValidator(validators.MaxLengthValidator):
message = 'Ensure this value has at most %(limit_value)d elements (it has %(show_value)d).'
@marceloleiva
marceloleiva / page.py
Created Jul 21, 2018 — forked from rturowicz/page.py
django - intermediate admin page
View page.py
# admin.py: admin action definition
def make_copy(self, request, queryset):
form = None
if 'apply' in request.POST:
form = CopyPageForm(request.POST)
if form.is_valid():
issue = form.cleaned_data['issue']
@marceloleiva
marceloleiva / django_model_graph.sh
Created Apr 3, 2018 — forked from perrygeo/django_model_graph.sh
Generate UML diagram of django app models
View django_model_graph.sh
apt-get install python-pygraphviz
pip install django-extensions
# add 'django_extensions' to INSTALLED_APPS in settings.py
python manage.py graph_models trees -o test.png
@marceloleiva
marceloleiva / letsencrypt_2017.md
Created Jun 6, 2017 — forked from cecilemuller/letsencrypt_2020.md
How to setup Let's Encrypt for Nginx on Ubuntu 16.04 (including IPv6, HTTP/2 and A+ SLL rating)
View letsencrypt_2017.md

How to setup Let's Encrypt for Nginx on Ubuntu 16.04 (including IPv6, HTTP/2 and A+ SLL rating)

There are two main modes to run the Let's Encrypt client (called Certbot):

  • Standalone: replaces the webserver to respond to ACME challenges
  • Webroot: needs your webserver to serve challenges from a known folder.

Webroot is better because it doesn't need to replace Nginx (to bind to port 80).

In the following, we're setting up mydomain.com. HTML is served from /var/www/mydomain, and challenges are served from /var/www/letsencrypt.

@marceloleiva
marceloleiva / views.py
Created Apr 27, 2017 — forked from MrYoda/views.py
Python 3, Django 1.9+: Excel file creation and send on-the-fly with XlsxWriter & BytesIO
View views.py
import xlsxwriter
from io import BytesIO
from django.http import StreamingHttpResponse
from django.views.generic import View
def get_foo_table_data():
"""
Some table data
"""
View django_reversion.md
pip install django-reversion

Add reversion to INSTALLED_APPS

INSTALLED_APPS = [
...
...
...
@marceloleiva
marceloleiva / forms.py
Created Dec 10, 2015 — forked from neara/forms.py
Django Class Based Views and Inline Formset Example
View forms.py
from django.forms import ModelForm
from django.forms.models import inlineformset_factory
from models import Sponsor, Sponsorship
class SponsorForm(ModelForm):
class Meta:
model = Sponsor
@marceloleiva
marceloleiva / bootstrap-tables2.css
Created Nov 20, 2015 — forked from shayh/bootstrap-tables2.css
bootstrap3 django-tables2 based on this gist https://gist.github.com/dyve/5458209 I created a template that works wtih https://github.com/dyve/django-bootstrap3 make sure you have 'django.core.context_processors.request' in your TEMPLATE_CONTEXT_PROCESSORS
View bootstrap-tables2.css
.table-container th.asc:after {
content: '\0000a0\0025b2';
}
.table-container th.desc:after {
content: '\0000a0\0025bc';
}
.pagination {
text-align: center;
}
@marceloleiva
marceloleiva / django_sleep.py
Last active Aug 29, 2015 — forked from josharian/django_sleep.py
Simple Django middleware that delays the processing of each request
View django_sleep.py
"""
This module provides very simple Django middleware that sleeps on every request.
This is useful when you want to simulate slow response times (as might be
encountered, say, on a cell network).
To use, add this middleware, and add a value for SLEEP_TIME to your settings.
Possible future feature: Look for an X-Django-Sleep header on each request,
to let the client specify per-request sleep time.