Created
April 5, 2017 12:42
-
-
Save Schlechtwetterfront/5d5528c539011e1c62257ac173d4c139 to your computer and use it in GitHub Desktop.
Django Snippets
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
''' | |
Middleware providing the current request for this thread. | |
Can be used to i.e. retrieve the current user: `user = current_request().user` | |
Needs to be added to the apps MIDDLEWARE list. | |
''' | |
from threading import current_thread | |
from django.utils.deprecation import MiddlewareMixin | |
_requests = {} | |
def current_request(): | |
return _requests.get(current_thread().ident, None) | |
class RequestMiddleware(MiddlewareMixin): | |
def process_request(self, request): | |
_requests[current_thread().ident] = request | |
def process_response(self, request, response): | |
# when response is ready, request should be flushed | |
_requests.pop(current_thread().ident, None) | |
return response | |
def process_exception(self, request, exception): | |
# if an exception has happened, request should be flushed too | |
_requests.pop(current_thread().ident, None) | |
raise exception |
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
''' | |
Simple models. | |
''' | |
from django.db import models | |
from django.urls import reverse, reverse_lazy | |
from django.forms import ModelForm, Textarea | |
class Post(models.Model): | |
def get_absolute_url(self): | |
return reverse_lazy('post', args=[str(self.id)]) | |
class PostForm(ModelForm): | |
class Meta: | |
model = Post | |
exclude = ('some_field', ) | |
localized_fields = '__all__' | |
widgets = { | |
'content': Textarea(attrs={'class': 'materialize-textarea', 'rows': 20}), | |
} | |
def save(self, commit=False): | |
inst = super(PostForm, self).save(commit=False) | |
# Do custom stuff. | |
if commit: | |
inst.save() | |
return inst |
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
''' | |
Simple views. | |
''' | |
from django.urls import reverse_lazy | |
from django.views import generic | |
from .models import Post, PostForm | |
class PostList(generic.ListView): | |
context_object_name = 'posts' | |
def get_queryset(self): | |
return Post.objects.all() | |
class PostView(generic.DetailView): | |
model = Post | |
class PostCreate(generic.CreateView): | |
model = Post | |
form_class = PostForm | |
class PostEdit(generic.UpdateView): | |
model = Post | |
fields = '__all__' | |
class PostDelete(generic.DeleteView): | |
model = Post | |
success_url = reverse_lazy('posts') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment