Django’s self.client.login(…) does not work in unit tests

The code that doesn’t work: from django.contrib.auth.models import User from django.test import Client user = User.objects.create(username=”testuser”, password=’12345′) c = Client() logged_in = c.login(username=”testuser”, password=’12345′) Why doesn’t it work? In the snippet above, when the User is created the actual password hash is set to be 12345. When the client calls the login method, the value … Read more

ImportError: Couldn’t import Django

I think the best way to use django is with virtualenv it’s safe and you can install many apps in virtualenv which does not affect any outer space of the system vitualenv uses the default version of python which is same as in your system to install virtualenv sudo pip install virtualenv or for python3 … Read more

Can I call a view from within another view?

Sure, as long as when it’s all said and done your view returns an HttpResponse object. The following is completely valid: def view1(request): # do some stuff here return HttpResponse(“some html here”) def view2(request): return view1(request) If you don’t want to return the HttpResponse from the first view then just store it into some variable … Read more

boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden

I’m using Amazon IAM for the particular key ID and access key and just bumped into the same 403 Forbidden… Turns out you need to give permissions that target both the bucket root and its subobjects: { “Statement”: [ { “Principal”: { “AWS”: “*” }, “Effect”: “Allow”, “Action”: “s3:*”, “Resource”: [“arn:aws:s3:::bucket-name/*”, “arn:aws:s3:::bucket-name”] } ] }

Django form with choices but also with freetext option?

I would recommend a custom Widget approach, HTML5 allows you to have a free text input with a dropdown list which would work as a pick-one-or-write-other type of field, this is how I made it: fields.py from django import forms class ListTextWidget(forms.TextInput): def __init__(self, data_list, name, *args, **kwargs): super(ListTextWidget, self).__init__(*args, **kwargs) self._name = name self._list … Read more

Django: Validate file type of uploaded file

Validating files is a common challenge, so I would like to use a validator: import magic from django.utils.deconstruct import deconstructible from django.template.defaultfilters import filesizeformat @deconstructible class FileValidator(object): error_messages = { ‘max_size’: (“Ensure this file size is not greater than %(max_size)s.” ” Your file size is %(size)s.”), ‘min_size’: (“Ensure this file size is not less than … Read more

Make sure only one worker launches the apscheduler event in a pyramid web app running multiple workers

Because Gunicorn is starting with 8 workers (in your example), this forks the app 8 times into 8 processes. These 8 processes are forked from the Master process, which monitors each of their status & has the ability to add/remove workers. Each process gets a copy of your APScheduler object, which initially is an exact … Read more

Django CSRF framework cannot be disabled and is breaking my site

Yes, Django csrf framework can be disabled. To manually exclude a view function from being handled by any CSRF middleware, you can use the csrf_exempt decorator, found in the django.views.decorators.csrf module. For example: (see doc) from django.views.decorators.csrf import csrf_exempt @csrf_exempt def my_view: return Httpresponse(“hello world”) ..and then remove {% csrf_token %} inside the forms from … Read more