A collection of tools to make setting up Django quicker.
Project description
Django-quicky
A collection of tools to make setting up Django quicker.
It is NOT a microframework and is meant to be used withing an ordinary Django setup as it's fully compatible with the standard usages.
You will love this tool if you ever wished you could do:
@url('/user/\d+')
@view(render_to='user.html')
def user_view(request, id):
# ...
return {'users': users}
@user_view.ajax(render_to='json')
def ajax_user_view(request, id, context):
# ...
return context
Note that this software is beta, but it's already used in production.
Just pip install django-quicky
.
Url decorators
If you like micro frameworks like bottle, you probably miss the very easy way to declare a route.
Now you can do this:
from django_quicky import routing
url, urlpatterns = routing()
@url('/any/regex/django/accepts')
def an_ordinary_view(request):
#...
@url('/you/can/stack/routing')
@url('/any/regex/django/accepts')
def an_ordinary_view(request):
# ...
Just declare your routes in the view. And use your view file in URL_ROOT
or any include()
like you would do with urls.py
.
Remember, order matters, so:
- views declared first will match first. Avoid declaring
@url(r'^$')
first (at the begining of views.py) or it will prevent others from matching. - when using several
@url
on the same view, the first applied (the lowest@url
in the decorators pile) will match first. - always put
@url
as the LAST decorator applied (at the very top of the decorators pile).
If you are in the mood for fancy stuff, and feel like adding a url manually, just do:
urlpatterns.add_url(url, view, [kwargs, name, prefix])
And for an include:
urlpatterns.include(url, view, [name, prefix])
And since you often add the admin url:
urlpatterns.add_admin(url)
Adding http error views is neither hard nor useful (most of the time), but for completeness:
@url.http404
def http404(request):
# ...
Of course, your view needs to return the proper status code.
View decorators
Rendering template and json bore you to death?
from django_quicky import view
@view(render_to='template.html')
def an_ordinary_view(request):
return {'stuff': stuff}
@view(render_to='json')
def a_json_view(request):
return {'stuff': stuff}
@view(render_to='raw')
def a_raw_view(request):
return 'hey'
For the first one, the returned dictionary will be used as a context (with RequestContext) to render the template. For the second one, it will be serialised to JSON. The last one will just return the string.
/!\ WARNING:
The view decorator should always be the first decorator to be applied (the lowest one in the decorator pile).
Conditional rendering
You can also declare alternatives based on a condition, for a single view:
from django_quicky import view
@view(render_to='template.html')
def common_views(request):
return {'stuff': stuff}
@common_views.post()
def post_view(request, context):
# do more stuff
return context
@common_views.ajax(render_to='json')
def json_view(request, context):
return context
The first view will be rendered as-is if it receives a normal GET request. The second view will be rendered only for POST requests, but will be passed the result of the execution of the first view. The second view will be rendered only for AJAX requests, and as JSON, but will be passed the result of the execution first view.
Just remember that alternative views must accept context
as a parameter, because they will always receive the result of the main view.
Oh, and of course you can define your own conditions:
@view(render_to='template.html')
def common_views(request):
return {'stuff': stuff}
@common_views.render_if(conditon=a_function_that_returns_a_bool)
def conditional_view(request, context):
# do more stuff
return context
Super user middleware
Double authentification? Short session timeout? Permission issue? Loooooooong password.
In, dev, just do:
if DEBUG:
MIDDLEWARE_CLASSES += (
'django_quicky.middleware.ForceSuperUserMiddleWare',
)
You will always be logged in as a super user. No password required. No timeout.
Serve static middleware
Serving static files IN DEV without worries:
if DEBUG:
MIDDLEWARE_CLASSES += (
'django_quicky.middleware.StaticServe',
)
And if you do want to test your site with DEBUG
set to False, you can just remove the condition.
The middleware accesses request.META['HTTP_HOST']
on requests but uses "django_quicky_fake_host" as a fallback for clients that don't provide it via headers (e.g: Django's test client). If you want to specify a different fallback host, you can do so by setting DJANGO_QUICKY_DEFAULT_HOST
in your settings.py file.
(Idea borrowed from the excellent django-annoying, but I stripped the internal test on DEBUG
which is a pain during testing.)
Settings context processor
Because everyone ends up needing access to the settings in his templates one day or the other:
TEMPLATE_CONTEXT_PROCESSORS = (
...
"django_quicky.context_processors.settings"
)
Loading settings
When you are not in Django, you may still want to import some django pieces, but they require a settings file.
This function make it easy to do so:
from django_quicky import load_config
load_config('/absolute/path/to/setting/file.py')
You can also call it with a relative path:
load_config('../../relative/path/to/setting/file.py')
But the starting point will be the one given with os.getcwd(), which is probably not what you want. You can force a starting point, most often you'll want the current file, by passing it manually:
load_config('../../relative/path/to/setting/file.py', starting_point=__file__)
starting_point
can be either a file (basename will be stripped) or a directory.
You can also pass a directory path, in which case Python will try to load a settings module from this directory:
load_config('/path/to/settings/directory')
It will attempt to load a module named as in os.environ['DJANGO_SETTINGS_MODULE']
or default to settings
. You can force the name by passing the settings_module
parameter.
DEBUGGING
The first rule when debugging decorators, is to be sure you use the right syntax: @decorator
and @decorator()
are very different and both syntaxically valid. In django-quickly's case, all decorators should be called with @decorator()
or @decorator(arguments)
.
Also remember that when it comes to decorators, order matters. Most of the time, you don't care about the order you apply your decorators, but in this case, you should ALWAYS apply @view
first and @url
last. E.G:
@url(r'$')
@login_required
@view('app/home.html')
def home(request):
# ...
If you don't do this, some decorators will never be executed as @view
bypasses decorators applied before it and @url
bypasses decorators after it.
Also, the order in which you declare your fonction matters, just like patterns order matters in urls.py
. So avoid putting global matching urls such as @url('^$')
at the begining of views.py
, otherwise this view will be used all the time, since the others will never have a chance to match.
Last words
There are other utility functions, but I didn't take the time to document them here, so you'll have to dig in the code. fields.py contains some useful model fields, utils.py has some shortcut functions and models.py comes with tools to get random entries or patch a model.
BTW, it's under the zlib license.
It embeds namegen, a name generator under BSD license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-quicky-0.7.3.tar.gz
.
File metadata
- Download URL: django-quicky-0.7.3.tar.gz
- Upload date:
- Size: 30.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6cf9a9e9e36b05a5251a92363efc8a80a467c77fd5a39b2ca4160ef7bbacc05c |
|
MD5 | 126391adadc5a0ab60ec2e594161074f |
|
BLAKE2b-256 | f16f8dfd02eebc6c638ba4f0b521c819cea1890af15cc13187fd5c81c778cfe9 |
File details
Details for the file django_quicky-0.7.3-py3-none-any.whl
.
File metadata
- Download URL: django_quicky-0.7.3-py3-none-any.whl
- Upload date:
- Size: 34.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 767dfdea5011aa7ee343aafa126a670d401d47248ce35a7e71de197c7b3f82df |
|
MD5 | 6bc82aea01b49e89b914d40fd8c59ced |
|
BLAKE2b-256 | fd98e36d59aa77bcf23df28dd8ca5e9962e07d3abfbd59e1b56762b1e5240a13 |