Categories
Python Answers

How to fix Python Django stops working with RuntimeError: populate() isn’t reentrant?

To fix Python Django stops working with RuntimeError: populate() isn’t reentrant, we can make a change to django/apps/registry.py.

To fix this, in django/apps/registry.py, we change

raise RuntimeError("populate() isn't reentrant")

with

self.app_configs = {}
Categories
Python Answers

How to access the request object or any other variable in a form’s clean() method with Python Django?

To access the request object or any other variable in a form’s clean() method with Python Django, we can use self.request in the clean method to access the request object.

For instance, we write

class MyForm(forms.Form):

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(MyForm, self).__init__(*args, **kwargs)


    def clean(self):
        self.request
        # ...  

to create the MyForm form with the clean method.

And then we can use self.request to access the request object in the clean method.

Categories
Python Answers

How to create a custom 500/404 error page with Python Django?

To create a custom 500/404 error page with Python Django, we can set the response object to the error code we want and return it.

For instance, we write

from django.shortcuts import render_to_response
from django.template import RequestContext


def handler404(request, *args, **argv):
    response = render_to_response('404.html', {},
                                  context_instance=RequestContext(request))
    response.status_code = 404
    return response


def handler500(request, *args, **argv):
    response = render_to_response('500.html', {},
                                  context_instance=RequestContext(request))
    response.status_code = 500
    return response

to call render_to_response with the template URL to create the response object.

And then we set the status_code to 404 or 500.

Then we return it.

Categories
Python Answers

How to filter a QuerySet with dynamic field lookups with Python Django?

To filter a QuerySet with dynamic field lookups with Python Django, we can pass in dynamic arguments to filter.

For instance, we write

kwargs = {
    '{0}__{1}'.format('name', 'startswith'): 'A',
    '{0}__{1}'.format('name', 'endswith'): 'Z'
}

Person.objects.filter(**kwargs)

to unpack the kwargs dictionary as arguments for filter so we can pass in any number of arguments we want into it.

Categories
Python Answers

How to redirect to previous page after login with Python Django?

To redirect to previous page after login with Python Django, we change some settings and then add the previous page URL into the template,

For instance, we write

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.request",
)

to add the django.core.context_processors.request and django.core.context_processors.auth template context processors.

Then we add the URL by writing

<a href="{% url django.contrib.auth.views.login %}?next={{request.path}}">Login</a>

to add the django.contrib.auth.views.login URL as the URL with the request.path path set to the redirect URL in base.html so the login form submit button will redirect to the request.path.

And then we add

{% block content %}
<form method="post" action="">
  {{form.as_p}}
<input type="submit" value="Login">
</form>
{% endblock %}

to add the login form.