Skip to content

Instantly share code, notes, and snippets.

View vitorfs's full-sized avatar

Vitor Freitas vitorfs

View GitHub Profile
@vitorfs
vitorfs / paginator.py
Last active October 14, 2016 07:59
Django's paginator
from django.contrib.auth.models import User
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
def index(request):
# here is the list of elements you want to paginate.
user_list = User.objects.all()
# `page` is a parameter we will receive from the url, like this:
# http://127.0.0.1:8000/users/?page=1
# http://127.0.0.1:8000/users/?page=2
@vitorfs
vitorfs / admin.py
Created October 11, 2016 17:46
Removing "sites" from Django's Flatpages
from django.contrib import admin
from django.contrib.flatpages.admin import FlatPageAdmin
from django.contrib.flatpages.models import FlatPage
from django.utils.translation import ugettext_lazy as _
class CustomFlatPageAdmin(FlatPageAdmin):
fieldsets = (
(None, {'fields': ('url', 'title', 'content',)}),
(_('Advanced options'), {
'classes': ('collapse',),
from django.db import transaction
@transaction.atomic
def create_user_view(request):
if request.method == 'POST':
user_form = UserCreationForm(request.POST)
profile_form = ProfileForm(request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.refresh_from_db() # This will load the Profile created by the Signal
@vitorfs
vitorfs / settings.py
Created August 25, 2016 16:52
template
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
PROJECT_DIR.child('templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
@vitorfs
vitorfs / views.py
Created August 19, 2016 22:13
Example
from django.contrib.auth.views import login as auth_login
from django.shortcuts import redirect
def login(request):
if request.user.is_author():
return redirect('/author/')
elif request.user.is_reader():
return redirect('/reader/')
@vitorfs
vitorfs / example.html
Created August 4, 2016 10:06
Django Bootstrap Pagination Widget
<table class="table table-bordered">
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>Date</th>
</tr>
</thead>
<tbody>
{% for article in articles %}
@vitorfs
vitorfs / example.html
Created August 4, 2016 09:59
Django Field Type Filter
{% load fieldtype %}
{% for field in form.visible_fields %}
{% if field|fieldtype == 'CheckboxInput' %}
...
{% elif field|fieldtype == 'CheckboxSelectMultiple' %}
...
{% endif %}
...
{% endfor %}
@vitorfs
vitorfs / example.html
Last active August 23, 2023 05:44
Startswith Template Filter
{% load startswith %}
<li{% if request.path|startswith:'/settings/' %} class="active"{% endif %}>
@vitorfs
vitorfs / change_password.html
Created August 4, 2016 09:08
Django Change Password View
<form method="post">
{% csrf_token %}
{{ form }}
<button type="submit">Save changes</button>
</form>
@vitorfs
vitorfs / recursion.py
Created July 10, 2016 12:52
Change Python Recursion Limit
import sys
# Get current limit
sys.getrecursionlimit() # Outputs 1000 (default)
# Set new limit
sys.setrecursionlimit(2000)