This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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',), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TEMPLATES = [ | |
{ | |
'BACKEND': 'django.template.backends.django.DjangoTemplates', | |
'DIRS': [ | |
PROJECT_DIR.child('templates'), | |
], | |
'APP_DIRS': True, | |
'OPTIONS': { | |
'context_processors': [ | |
'django.template.context_processors.debug', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table class="table table-bordered"> | |
<thead> | |
<tr> | |
<th>Title</th> | |
<th>Author</th> | |
<th>Date</th> | |
</tr> | |
</thead> | |
<tbody> | |
{% for article in articles %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% load fieldtype %} | |
{% for field in form.visible_fields %} | |
{% if field|fieldtype == 'CheckboxInput' %} | |
... | |
{% elif field|fieldtype == 'CheckboxSelectMultiple' %} | |
... | |
{% endif %} | |
... | |
{% endfor %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% load startswith %} | |
<li{% if request.path|startswith:'/settings/' %} class="active"{% endif %}> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form method="post"> | |
{% csrf_token %} | |
{{ form }} | |
<button type="submit">Save changes</button> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
# Get current limit | |
sys.getrecursionlimit() # Outputs 1000 (default) | |
# Set new limit | |
sys.setrecursionlimit(2000) |