django account helper utils for `django.contrib.auth`
Project description
django account helper utils for django.contrib.auth
Requirement
Django1.8+
Install
pip install django_acocunt_helper
Config
check your settings.py, make sure django.contrib.auth in INSTALLED_APPS.
add account_helper.middleware.CurrentUserMiddleware
config finish.
How to use
set current user as default value
update your model like this:
before
owner = models.ForeignKey(settings.AUTH_USER_MODEL)
after
from account_helper.middleware import get_current_user
# ... fields definition...
owner = models.ForeignKey(settings.AUTH_USER_MODEL, default=get_current_user, null=True)
set current user id as default value
update your model like this:
before
owner = models.IntegerField('user id')
after
from account_helper.middleware import get_current_user_id
# ... fields definition...
owner = models.IntegerField('user id',default=get_current_user_id)
use session in form
sometimes we really need handle session in form. but in django. we have to do something like this.
old style
in your views. set get_form_kwargs
class YourFormView(FormView):
form_class=YourForm
def get_form_kwargs(self):
return {'request':self.request}
pass
pass
2.set your form.
Class YourForm(forms.Form):
request = None
def __init__(self,*args,**kwargs):
self.request = kwargs.get('request')
if 'request' in kwargs:
del kwargs['request']
super (YourForm,self).__init__(*args,**kwargs)
do it in django-account-helper
from account_helper.middleware import get_current_session
Class YourForm(forms.Form):
def clean(self):
session = get_current_session()
if self.cleaned_data.get('foo') == session.get('foo'):
# do something
pass
#... your code
pass
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Close
Hashes for django-account-helper-0.1.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b45c3098b7282bd7b26814a9c95cae2aa57293799ca7793c8d8aa4390cedeb4 |
|
MD5 | aa313a7c365de1c68aacd83bc7fb74c9 |
|
BLAKE2b-256 | 8d59045b47ca4b63a4fe0139fe032d6d4439613c83eb3bd957ea94ffde4c99d7 |