Created
May 22, 2012 12:15
-
-
Save sneeu/2768694 to your computer and use it in GitHub Desktop.
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
def beta_access(request): | |
if request.method == 'POST': | |
form = BetaCodeForm(request.POST) | |
# form validation handles checking for the beta code | |
if form.is_valid(): | |
request.session['has_beta_access'] = True | |
return redirect('register') | |
else: | |
form = BetaCodeForm(request.POST) | |
return TemplateResponse(request, 'beta_access.html', context={'form': form}) | |
def beta_access_required(view): | |
def f(request, *args, **kwargs): | |
if request.session.get('has_beta_access', None) != True: | |
return redirect('beta_access') | |
return view(request, *args, **kwargs) | |
return f |
@pyrat additionally, in Django if you wanted to do something before saving a model, you overwrite the save
method, and eventually call super
, generally you’re not bothered about what save
is called with, so you’d use *args
, and **kwargs
in that instance too.
An example:
class TimestampedModel(models.Model):
last_updated = models.DatetimeField(blank=True, null=True)
def save(self, *args, **kwargs):
# Not too bothered about what args are used, just throw them up the inheritance tree.
self.last_updated = datetime.datetime.utcnow()
super(TimestampedModel, self).save(*args, **kwargs)
Nice, thanks for the rundown. Hopefully I will find an excuse to write some Python in the near future.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@pyrat there are quite a few reasons, Jinja2 uses the keyword arguments to define the context in a template.
In the example above, we’re defining a decorator, which is used:
The original
someview
function is wrapped by thebeta_access_required
decorator, which in the example replaces it withf
(which has a closure overview
, so the originalview
can be called from within).But the
beta_access_required
function doesn’t really care about the arguments, so it can pass them straight through, image having to write a differentbeta_access_required
for every view function signature.Django’s
user_passes_test
decorator (which is whatlogin_required
is) is similar.Python also has the syntactic sugar:
Which is the same as the above.