If all of all my Django apps are in the "apps" dir, is there something I can set so that I can directly write the app name instead of "apps.app_name" everytime.
It seems like I can do this only with sys.path or PYTHONPATH, which applies to all the django projects in the environment. This doesn't seem like a satisfactory solution. ... my summer project MozSecWorld didn't have this problem, I wonder what magic the setup for that did.
Showing posts with label django. Show all posts
Showing posts with label django. Show all posts
Friday, December 16, 2011
Setting up new Django project (with virtualenv(wrapper))
Download Django for Ubuntu in root:
1.
2a.
2b.
2c.
So my Django path is:
In user:
1.
2. Since I installed with no-site-packages, I need to add Django to the virtualenv
3. follow the Django tutorial
3b. In
3c. I installed my app inside an "apps" folder, so here is what I did:
Note that you should have 'apps.app_name' in INSTALLED_APPS or else it's not going to find it and give you this error instead:
1.
cd /usr/local/lib, mkdir django-1.3.1 and cd into it2a.
wget http://www.djangoproject.com/download/1.3.1/tarball/ -O Django-1.3.1.tar.gz, got this step from here2b.
cd Django-1.3.1.tar.gz2c.
sudo python setup.py installSo my Django path is:
/usr/local/lib/django-1.3.1/Django-1.3.1
In user:
1.
mkvirtualenv --no-site-packages vocab this is virtualenvwrapper setting up a new environment. Setup is logged in this old post.$ mkvirtualenv --no-site-packages vocab
New python executable in vocab/bin/python
Installing setuptools............done.
Installing pip...............done.
virtualenvwrapper.user_scripts creating /home/my_username/.virtualenvs/vocab/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/my_username/.virtualenvs/vocab/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/my_username/.virtualenvs/vocab/bin/preactivate
virtualenvwrapper.user_scripts creating /home/my_username/.virtualenvs/vocab/bin/postactivate
virtualenvwrapper.user_scripts creating /home/my_username/.virtualenvs/vocab/bin/get_env_details
mkdir: cannot create directory `/home/my_username/Projects/vocab': No such file or directory
-bash: cd: /home/my_username/Projects/vocab: No such file or directory
Searching for readline
Reading http://pypi.python.org/simple/readline/
Reading http://www.python.org/
Reading http://github.com/ludwigschwardt/python-readline
Best match: readline 6.2.1
Downloading http://pypi.python.org/packages/source/r/readline/readline-6.2.1.tar.gz#md5=9604527863378512247fcaf938100797
Processing readline-6.2.1.tar.gz
Running readline-6.2.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-9h67Mv/readline-6.2.1/egg-dist-tmp-nNoSrj
/usr/bin/ld: cannot find -lncurses
collect2: ld returned 1 exit status
error: Setup script exited with error: command 'gcc' failed with exit status 1
Downloading/unpacking Jinja2==2.3.1
Downloading Jinja2-2.3.1.tar.gz (428Kb): 428Kb downloaded
Running setup.py egg_info for package Jinja2
warning: no previously-included files matching '*' found under directory 'docs/_build/doctrees'
Downloading/unpacking MySQL-python==1.2.3c1
Downloading MySQL-python-1.2.3c1.tar.gz (89Kb): 89Kb downloaded
Running setup.py egg_info for package MySQL-python
Downloading/unpacking lxml==2.2.6
Downloading lxml-2.2.6.tar.gz (2.9Mb): 2.9Mb downloaded
Running setup.py egg_info for package lxml
Building lxml version 2.2.6.
NOTE: Trying to build without Cython, pre-generated 'src/lxml/lxml.etree.c' needs to be available.
ERROR: /bin/sh: xslt-config: not found
** make sure the development packages of libxml2 and libxslt are installed **
Using build configuration of libxslt
warning: no previously-included files found matching '*.py'
Downloading/unpacking PIL
Downloading PIL-1.1.7.tar.gz (506Kb): 506Kb downloaded
Running setup.py egg_info for package PIL
WARNING: '' not a valid package name; please use only.-separated package names in setup.py
Obtaining check from git+git://github.com/jbalogh/check.git#egg=check
Cloning git://github.com/jbalogh/check.git to /home/my_username/.virtualenvs/vocab/src/check
Running setup.py egg_info for package check
Downloading/unpacking pyflakes (from check)
Downloading pyflakes-0.5.0.tar.gz
Running setup.py egg_info for package pyflakes
Downloading/unpacking pep8 (from check)
Downloading pep8-0.6.1.tar.gz
Running setup.py egg_info for package pep8
Downloading/unpacking path.py (from check)
Error while getting http://pypi.python.org/packages/source/p/path.py/path.py-2.2.2.tar.gz#md5=3a3262b9049d294ddbd6f7453d45e699 (from http://pypi.python.org/simple/path.py/)
Exception:
Traceback (most recent call last):
File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/basecommand.py", line 126, in main
self.run(options, args)
File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/commands/install.py", line 223, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/req.py", line 961, in prepare_files
self.unpack_url(url, location, self.is_download)
File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/req.py", line 1079, in unpack_url
return unpack_http_url(link, location, self.download_cache, only_download)
File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/download.py", line 429, in unpack_http_url
resp = _get_response_from_url(target_url, link)
File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/download.py", line 465, in _get_response_from_url
resp = urlopen(target_url)
File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/download.py", line 84, in __call__
response = urllib2.urlopen(self.get_request(url))
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 391, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 409, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1185, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1160, in do_open
raise URLError(err)
URLError:
Storing complete log in /home/my_username/.pip/pip.log
2. Since I installed with no-site-packages, I need to add Django to the virtualenv
add2virtualenv /usr/local/lib/django-1.3.1/Django-1.3.13. follow the Django tutorial
3b. In
python manage.py sql app_name I got this error:...
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
so I didpip install MySQL-python3c. I installed my app inside an "apps" folder, so here is what I did:
mkdir apps; touch apps/__init__.pypython manage.py startapp app_namemv app_name appsvim settings.py and add into INSTALLED_APPS 'apps.apps_name'python manage.py sql app_nameNote that you should have 'apps.app_name' in INSTALLED_APPS or else it's not going to find it and give you this error instead:
Error: No module named app_name
Friday, October 21, 2011
Setting up MSW on Rackspace
./manage.py shelldjango.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
In root install:
pip install mysql-python. If you I got another error ...
In file included from _mysql.c:29:0:
pymemcompat.h:10:20: fatal error: Python.h: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1
... then you need to install pymemcompat.h:10:20: fatal error: Python.h: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1
sudo apt-get install python-dev(I searched for solutions for a long time, trying to find my "/usr/local/mysql" on ubuntu, but I don't have it (even though I have mysql).)
Now
./manage.py shell hasImportError: No module named OpenSSL
From root, pip install pyopensslNow
./manage.py shell hasImportError: No module named bycrypt
From root, pip install py-bcryptAND THEN I GOT MANAGE.PY SHELL!!!
import django! works on it too!!!!!!!!
$ ./manage.py shell Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> import django
Whoa I can even run it on my rackspace IP! :D Even though it crashes, at least it can get the request!
$ ./manage.py runserver 50.57.147.82:8000
Validating models...
0 errors found
Django version 1.3.1, using settings 'MozSecWorld.settings_local'
Development server is running at http://50.57.147.82:8000/
Quit the server with CONTROL-C.
[21/Oct/2011 03:23:36] "GET / HTTP/1.1" 301 0
Traceback (most recent call last):
...
TemplateSyntaxError: Caught ImportError while rendering: No module named jinja2
[21/Oct/2011 03:23:37] "GET /msw/ HTTP/1.1" 500 2909
So I did, from root, pip install jinja2AND MOZSECWORLD IS UP at http://50.57.147.82:8000/!!!!!!!! :D:D:D
Wednesday, August 3, 2011
Django vendor-local adding package
playdoh (master)$ pip install --no-install --build=vendor-local/packages --src=vendor-local/src -I django-csp
Downloading/unpacking django-csp
Downloading django_csp-1.0.2.tar.gz
Running setup.py egg_info for package django-csp
which installed django-csp into vendor-local/packages
and then I added the path to vendor-local/vendor.pth:
packages/django-csp
FYI, the contents:
playdoh/vendor-local/packages/django-csp (master)$ ls -R PKG-INFO django_csp.egg-info setup.cfg csp pip-egg-info setup.py ./csp: __init__.py decorators.py middleware.py urls.py views.py ./django_csp.egg-info: PKG-INFO dependency_links.txt SOURCES.txt top_level.txt ./pip-egg-info: django_csp.egg-info ./pip-egg-info/django_csp.egg-info: PKG-INFO dependency_links.txt SOURCES.txt top_level.txt
The Resource
Wednesday, July 27, 2011
Python Check.py!
pip install -e git://github.com/jbalogh/check.git#egg=check
Pip Install Django Debug Toolbar
pip install django-debug-toolbarSo you get to do
python manage.py debugsqlshell and have a debug sidebar.It's awesome
Pypi
Tuesday, July 26, 2011
Randomizing in Django QuerySet and Python List
Django QuerySet:
Python List:
And then you can use slice to just keep n elements, e.g.
my_queryset.order_by('?')
Python List:
import random random.shuffle(my_list)
And then you can use slice to just keep n elements, e.g.
n = 5
my_queryset.order_by('?')[:n]
Monday, July 25, 2011
Django "MultipleObjectsReturned: get() returned more than one" fix with filter()
get() returns single objects. so use filter() instead.
Sunday, July 24, 2011
Django get user object in views.py
In views.py
def index(request):
# get user
try:
the_user = User.objects.get(username = request.user)
except User.DoesNotExist:
return HttpResponse("Invalid username")
Saturday, July 23, 2011
How adopted-kitsune Django upload works.
0. forms.py
1. views.py
for fileupload page if request.method == 'POST':
form = forms.ImageAttachmentUploadForm(request.POST, request.FILES)
if form.is_valid()
2. playdoh/vendor/src/django/django/forms/fields.py
class ImageField(FileField):
def to_python(self, data):
"""
Checks that the file-upload field data contains a valid image (GIF, JPG,
PNG, possibly others -- whatever the Python Imaging Library supports).
"""
class ImageAttachmentUploadForm(forms.Form):
"""Image upload form."""
print "############################in form"
image = forms.ImageField(error_messages={'required': MSG_IMAGE_REQUIRED,
'max_length': MSG_IMAGE_LONG},
max_length=settings.MAX_FILENAME_LENGTH)
1. views.py
for fileupload page if request.method == 'POST':
form = forms.ImageAttachmentUploadForm(request.POST, request.FILES)
if form.is_valid()
2. playdoh/vendor/src/django/django/forms/fields.py
class ImageField(FileField):
def to_python(self, data):
"""
Checks that the file-upload field data contains a valid image (GIF, JPG,
PNG, possibly others -- whatever the Python Imaging Library supports).
"""
Wednesday, July 20, 2011
Getting Image Upload to work on Django
Learned form r1cky that jpeg and PIL are packages that need to be recompiled for other people, especially those on different operating systems
haoqili: how can I tell if a package needs to be recompiled on the VM or not?
r1cky: hmm, that is a good question. if it is only .py source files then it doesnt need compiling
r1cky: but some libraries have C in them
r1cky: the ones that we use that need compiling are
r1cky: MySQL-python, Jinja2, PIL, lxml
r1cky: and the jpeg one
r1cky: those are installed separately (not copied into the repository)
1. Install PIL into my vendor-local
2. Trying to get JPEG to be supported:
3. Rebuild PIL
haoqili: how can I tell if a package needs to be recompiled on the VM or not?
r1cky: hmm, that is a good question. if it is only .py source files then it doesnt need compiling
r1cky: but some libraries have C in them
r1cky: the ones that we use that need compiling are
r1cky: MySQL-python, Jinja2, PIL, lxml
r1cky: and the jpeg one
r1cky: those are installed separately (not copied into the repository)
1. Install PIL into my vendor-local
playdoh (master)$ pip install -I --install-option="--home=`pwd`/vendor-local" pil
Downloading/unpacking pil
Downloading PIL-1.1.7.tar.gz (506Kb): 506Kb downloaded
Running setup.py egg_info for package pil
WARNING: '' not a valid package name; please use only.-separated package names in setup.py
WARNING: '' not a valid package name; please use only.-separated package names in setup.py
Installing collected packages: pil
Running setup.py install for pil
WARNING: '' not a valid package name; please use only.-separated package names in setup.py
WARNING: '' not a valid package name; please use only.-separated package names in setup.py
--- using frameworks at /System/Library/Frameworks
building '_imaging' extension
/usr/bin/cc -fno-strict-aliasing -O3 -w -pipe -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -DHAVE_LIBZ -I/System/Library/Frameworks/Tcl.framework/Headers -I/System/Library/Frameworks/Tk.framework/Headers -IlibImaging -I/Users/haoqili/.virtualenvs/playdoh/include -I/usr/local/include -I/usr/include -I/usr/local/Cellar/python/2.7.1/include/python2.7 -c _imaging.c -o build/temp.macosx-10.4-x86_64-2.7/_imaging.o
...
/usr/bin/cc -L/usr/local/Cellar/readline/6.2.1/lib -bundle -undefined dynamic_lookup -L/usr/local/Cellar/readline/6.2.1/lib build/temp.macosx-10.4-x86_64-2.7/_imagingmath.o -L/usr/local/lib -L/Users/haoqili/.virtualenvs/playdoh/lib -L/usr/lib -o build/lib.macosx-10.4-x86_64-2.7/_imagingmath.so
--------------------------------------------------------------------
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version 1.1.7
platform darwin 2.7.1 (r271:86832, Jun 6 2011, 13:57:48)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
--------------------------------------------------------------------
--- TKINTER support available
*** JPEG support not available
--- ZLIB (PNG/ZIP) support available
*** FREETYPE2 support not available
*** LITTLECMS support not available
--------------------------------------------------------------------
To add a missing option, make sure you have the required
library, and set the corresponding ROOT variable in the
setup.py script.
To check the build, run the selftest.py script.
changing mode of build/scripts-2.7/pilconvert.py from 644 to 755
changing mode of build/scripts-2.7/pildriver.py from 644 to 755
changing mode of build/scripts-2.7/pilfile.py from 644 to 755
changing mode of build/scripts-2.7/pilfont.py from 644 to 755
changing mode of build/scripts-2.7/pilprint.py from 644 to 755
changing mode of /Users/haoqili/dev/playdoh/playdoh/playdoh/vendor-local/bin/pilconvert.py to 755
changing mode of /Users/haoqili/dev/playdoh/playdoh/playdoh/vendor-local/bin/pildriver.py to 755
changing mode of /Users/haoqili/dev/playdoh/playdoh/playdoh/vendor-local/bin/pilfile.py to 755
changing mode of /Users/haoqili/dev/playdoh/playdoh/playdoh/vendor-local/bin/pilfont.py to 755
changing mode of /Users/haoqili/dev/playdoh/playdoh/playdoh/vendor-local/bin/pilprint.py to 755
Successfully installed pil
Cleaning up...
2. Trying to get JPEG to be supported:
playdoh/vendor-local (master)$ brew install jpeg Warning: Xcode is not installed! Builds may fail! ==> Downloading http://www.ijg.org/files/jpegsrc.v8c.tar.gz ######################################################################## 100.0% ==> ./configure --prefix=/usr/local/Cellar/jpeg/8c --disable-dependency-tracking ==> make install /usr/local/Cellar/jpeg/8c: 17 files, 1.6M, built in 13 seconds
3. Rebuild PIL
)$ pip install PIL==1.1.7 --upgrade
Downloading/unpacking PIL==1.1.7
Downloading PIL-1.1.7.tar.gz (506Kb): 506Kb downloaded
Running setup.py egg_info for package PIL
WARNING: '' not a valid package name; please use only.-separated package names in setup.py
WARNING: '' not a valid package name; please use only.-separated package names in setup.py
Installing collected packages: PIL
Found existing installation: PIL 1.1.7
Uninstalling PIL:
Successfully uninstalled PIL
Running setup.py install for PIL
WARNING: '' not a valid package name; please use only.-separated package names in setup.py
WARNING: '' not a valid package name; please use only.-separated package names in setup.py
--- using frameworks at /System/Library/Frameworks
building '_imaging' extension
/usr/bin/cc -fno-strict-aliasing -O3 -w -pipe -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -DHAVE_LIBJPEG -DHAVE_LIBZ -I/System/Library/Frameworks/Tcl.framework/Headers -I/System/Library/Frameworks/Tk.framework/Headers -IlibImaging -I/Users/haoqili/.virtualenvs/playdoh/include -I/usr/local/include -I/usr/include -I/usr/local/Cellar/python/2.7.1/include/python2.7 -c _imaging.c -o build/temp.macosx-10.4-x86_64-2.7/_imaging.o
...
/usr/bin/cc -L/usr/local/Cellar/readline/6.2.1/lib -bundle -undefined dynamic_lookup -L/usr/local/Cellar/readline/6.2.1/lib build/temp.macosx-10.4-x86_64-2.7/_imagingmath.o -L/usr/local/lib -L/Users/haoqili/.virtualenvs/playdoh/lib -L/usr/lib -o build/lib.macosx-10.4-x86_64-2.7/_imagingmath.so
--------------------------------------------------------------------
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version 1.1.7
platform darwin 2.7.1 (r271:86832, Jun 6 2011, 13:57:48)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
--------------------------------------------------------------------
--- TKINTER support available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
*** FREETYPE2 support not available
*** LITTLECMS support not available
--------------------------------------------------------------------
To add a missing option, make sure you have the required
library, and set the corresponding ROOT variable in the
setup.py script.
To check the build, run the selftest.py script.
changing mode of build/scripts-2.7/pilconvert.py from 644 to 755
changing mode of build/scripts-2.7/pildriver.py from 644 to 755
changing mode of build/scripts-2.7/pilfile.py from 644 to 755
changing mode of build/scripts-2.7/pilfont.py from 644 to 755
changing mode of build/scripts-2.7/pilprint.py from 644 to 755
changing mode of /Users/haoqili/.virtualenvs/playdoh/bin/pilconvert.py to 755
changing mode of /Users/haoqili/.virtualenvs/playdoh/bin/pildriver.py to 755
changing mode of /Users/haoqili/.virtualenvs/playdoh/bin/pilfile.py to 755
changing mode of /Users/haoqili/.virtualenvs/playdoh/bin/pilfont.py to 755
changing mode of /Users/haoqili/.virtualenvs/playdoh/bin/pilprint.py to 755
Successfully installed PIL
Cleaning up...
Tuesday, July 19, 2011
Django Playdoh Package Locations
I was confused when reading how to add packages and seeing that packages live in different locations.
jsocol clarified:
/packages is old-and-busted, use lib/python for non-git things
but you can't put any libraries with compiled components into the vendor lib (it means vendor can't contain anything with c/c++ components)
it's just a question of installing via git (or, well, pip install vcs+vcs://) or using PyPI and the name (e.g. "pip install foo")
This is what I did, with help from groovecoder. :D
And then I found that cef is inside vendor-local/packages. :D
Now I need to add this path to vendor.pth. Groovecoder told me that "*.pth files are automatically included by the python interpreter"
Now I can use cef!
I could also have done
jsocol clarified:
/packages is old-and-busted, use lib/python for non-git things
but you can't put any libraries with compiled components into the vendor lib (it means vendor can't contain anything with c/c++ components)
it's just a question of installing via git (or, well, pip install vcs+vcs://) or using PyPI and the name (e.g. "pip install foo")
pip install foo gives vendor-local/lib/python/foousing git to install foo gives vendor-local/src/fooThis is what I did, with help from groovecoder. :D
playdoh (master)$ pip install --no-install --build=vendor-local/packages --src=vendor-local/src -I cef
Downloading/unpacking cef
Downloading cef-0.2.tar.gz
Running setup.py egg_info for package cef
Successfully downloaded cef
And then I found that cef is inside vendor-local/packages. :D
Now I need to add this path to vendor.pth. Groovecoder told me that "*.pth files are automatically included by the python interpreter"
playdoh/vendor-local (master)$ cat vendor.pth packages/cef
Now I can use cef!
playdoh (master)$ python manage.py shell ... >>> from cef import log_cef >>>
I could also have done
/playdoh (master)$ pip install -I --install-option="--home=`pwd`/vendor-local" cef
Downloading/unpacking cef
Downloading cef-0.2.tar.gz
Running setup.py egg_info for package cef
Installing collected packages: cef
Running setup.py install for cef
Successfully installed cef
Cleaning up...
and haveplaydoh/vendor-local/lib/python (master)$ ls cef-0.2-py2.7.egg-info cef.py cef.pyc
Django urls, best practice
First iteration:
template html:
Second iteration:
template html:
urls.py:
Third (best) iteration
template html:
inspiration
template html:
<a href="/msw/{{ page.slug }}/">back</a><
Second iteration:
template html:
<a href="{{ url('detail', 'richtext_and_safe_url') }}">back</a>
with urls.py:
url(r'^(?P<input_slug>\w+)/$', views.detail, name='detail')
Third (best) iteration
template html:
<h1><a href="{{ page.reverse_url() }}">back</a></h1>
in models.py:class Page(models.Model):
slug = models.SlugField() # another way
....
def reverse:_url(self):
return reverse('detail', args=[self.slug])
#return reverse('detail', args=['richtext_and_safe_url']) # hard coded way
be sure to pass in "page" in your views.pyinspiration
Thursday, July 14, 2011
Django debug
import pdb; pdb.set_trace()
Django preview database query/queries before they are performed
python manage.py sqlall [app name]
Wednesday, July 13, 2011
Django shell add user permissions and group permissions
Django doc
1. Start the shell, go to your maange.py
2. Get the user
Either load one ...
Or create one ...
3. Check the user's permissions
1. Start the shell, go to your maange.py
2. Create the group
NOTE that Django pre-made permissions are only suggestions, they only go as far as their string names. YOU have to implement them if you want to use them.
1. Start the shell, go to your maange.py
>>> python manage.py shell
2. Get the user
Either load one ...
>>> from django.contrib.auth import authenticate >>> user = authenticate(username="johndoe", password="yourpw")
Or create one ...
>>> from django.contrib.auth.models import User >>> user = User.objects.create_user(username='johndoe', email="", password="yourpw")Django should silently automatically save it, and you should see it under Users in your admin page. But if you want save it yourself, do:
>>> user.is_staff = False >>> user.save
3. Check the user's permissions
user.get_all_permissions()or
user.user_permissions.all()
1. Start the shell, go to your maange.py
>>> python manage.py shell
2. Create the group
>>> from django.contrib.auth.models import Group >>> group = Group(name="GroupName") >>> group.save()
NOTE that Django pre-made permissions are only suggestions, they only go as far as their string names. YOU have to implement them if you want to use them.
Monday, July 11, 2011
Django reference/link to media/js from apps/myapp/views.py
I had to change a javascript file in media/js/my.js on every refresh of a page (apps/myapp/views.py), the path to open that js file is:
See a live demo of this here.
f = open('media/js/my.js', 'r+').See a live demo of this here.
Google recaptcha with CSP, hosting javascript locally
I display google's recaptcha from google's recaptcha javascript hosted on my own server after a little manipulation (since I have CSP turned on, I have to bypass the in-body script and setInterval). But I still need to ping google every time for a new challenge.
1. So I saved my domain's equivalent of the RecaptchaState javascript medai/js/google/recState.js.
2. Refresh this file in views.py's by calling this function:
3. Copy paste recaptcha.js and make the following changes for CSP compatibility:
3.1 CSP blocks setIntervals that takes string parameters, so change it into a function:
3.2 CSP blocks in-body javascript, host it externally
4. and in your template.html include the javascript from step 2 and 3.
5. Change your settings.py's CSP policies to have "http[s]://www.google.com" allowed in many places. See example
1. So I saved my domain's equivalent of the RecaptchaState javascript medai/js/google/recState.js.
2. Refresh this file in views.py's by calling this function:
def recaptchaRefresh():
# get the Recaptcha state.
url = "https://www.google.com/recaptcha/api/challenge?k=%s" % settings.RECAPTCHA_PUBLIC_KEY
resock = urllib.urlopen(url)
data = resock.read()
resock.close()
# extract the recaptcha state part of the string
docloc = data.find("document.write")
recaptchaState = data[:docloc]
f = open('media/js/google/recState.js', 'r+')
f.write(recaptchaState)
f.close()
3. Copy paste recaptcha.js and make the following changes for CSP compatibility:
3.1 CSP blocks setIntervals that takes string parameters, so change it into a function:
//Recaptcha.timer_id = setInterval("Recaptcha.reload('t');", (a.timeout - 300) * 1E3)
// -->
Recaptcha.timer_id = setInterval( function() {Recaptcha.reload('t'); }, (a.timeout - 300) * 1E3)
3.2 CSP blocks in-body javascript, host it externally
//} else document.write('<div id="recaptcha_widget_div" style="display:none"></div>'), document.write('<script>Recaptcha.widget = Recaptcha.$("recaptcha_widget_div"); Recaptcha.challenge_callback();<\/script>');
// -->
} else document.write('<div id="recaptcha_widget_div" style="display:none"></div>'), document.write('<script src="http://haoqili.scripts.mit.edu/js/test3.js"><\/script>');
where http://haoqili.scripts.mit.edu/js/test3.js has "Recaptcha.widget = Recaptcha.$("recaptcha_widget_div"); Recaptcha.challenge_callback();"
4. and in your template.html include the javascript from step 2 and 3.
5. Change your settings.py's CSP policies to have "http[s]://www.google.com" allowed in many places. See example
Saturday, July 9, 2011
Find where a module is
Run python manage.py shell.
Import captcha.fields
It's actually here: django-recaptcha already done before!!, not here. I wish I had know that the django-recaptcha existed before I did this.
Import captcha.fields
help(captcha.fields) or captcha.__file__ or captcha.fields shown below
In [3]: import captcha.fields
In [4]: captcha.fields?
Type: module
Base Class: <type 'module'>
String Form: <module 'captcha.fields' from '/Users/jbalogh/dev/zamboni/vendor/src/django-recaptcha/captcha/fields.pyc'>
Namespace: Interactive
File: /Users/uname/dev/zamboni/vendor/src/django-recaptcha/captcha/fields.py
Docstring:
<no docstring>
It's actually here: django-recaptcha already done before!!, not here. I wish I had know that the django-recaptcha existed before I did this.
Friday, July 8, 2011
How Django form calls clean() method
I was reading the Django docs on forms which says:
Validation of a Form is split into several steps ... [calls]
- [1] to_python()
- [2] validate()
- [3] run_validators()
- [4] clean()
- [5] clean_()
This is how these things work in the source code:
1. each field's clean() method takes care of the first 3 items
2. aoeu
... calls some alias ...
figure it out yourself
Validation of a Form is split into several steps ... [calls]
- [1] to_python()
- [2] validate()
- [3] run_validators()
- [4] clean()
- [5] clean_
This is how these things work in the source code:
1. each field's clean() method takes care of the first 3 items
# src/django/django/forms/fields.py, comments mine
class Field(object):
...
def clean(self, value):
"""
Validates the given value and returns its "cleaned" value as an
appropriate Python object.
Raises ValidationError for any errors.
"""
value = self.to_python(value) # [1]
self.validate(value) # [2]
self.run_validators(value) # [3]
return value
2. aoeu
# src/django/django/forms/fields.py, comments mine
class BaseForm(StrAndUnicode):
...
def full_clean(self):
"""
Cleans all of self.data and populates self._errors and
self.cleaned_data.
"""
self._errors = ErrorDict()
if not self.is_bound: # Stop further processing.
return
self.cleaned_data = {}
# If the form is permitted to be empty, and none of the form data has
# changed from the initial data, short circuit any validation.
if self.empty_permitted and not self.has_changed():
return
self._clean_fields()
self._clean_form()
self._post_clean()
if self._errors:
del self.cleaned_data
def _clean_fields(self):
for name, field in self.fields.items():
# value_from_datadict() gets the data from the data dictionaries.
# Each widget type knows how to retrieve its own data, because some
# widgets split data over several HTML fields.
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
try:
if isinstance(field, FileField):
initial = self.initial.get(name, field.initial)
# calls step 1's stuff
value = field.clean(value, initial)
else:
value = field.clean(value)
self.cleaned_data[name] = value
if hasattr(self, 'clean_%s' % name):
value = getattr(self, 'clean_%s' % name)()
self.cleaned_data[name] = value
except ValidationError, e:
self._errors[name] = self.error_class(e.messages)
if name in self.cleaned_data:
del self.cleaned_data[name]
... calls some alias ...
figure it out yourself
Subscribe to:
Posts (Atom)