Testing in Django

Automated testing is an extremely useful bug-killing tool for the modern Web developer. You can use a collection of tests – a test suite – to solve, or avoid, a number of problems: When you’re writing new code, you can use tests to validate your code works as expected. When you’re refactoring or modifying old code, you can use tests to ensure your changes haven’t affected your application’s behavior unexpectedly. Testing a Web application is a complex task, because a Web application is made o

http.HttpRequest.POST

HttpRequest.POST A dictionary-like object containing all given HTTP POST parameters, providing that the request contains form data. See the QueryDict documentation below. If you need to access raw or non-form data posted in the request, access this through the HttpRequest.body attribute instead. It’s possible that a request can come in via POST with an empty POST dictionary – if, say, a form is requested via the POST HTTP method but does not include form data. Therefore, you shouldn’t use if

db.transaction.set_autocommit()

set_autocommit(autocommit, using=None) [source] These functions take a using argument which should be the name of a database. If it isn’t provided, Django uses the "default" database. Autocommit is initially turned on. If you turn it off, it’s your responsibility to restore it. Once you turn autocommit off, you get the default behavior of your database adapter, and Django won’t help you. Although that behavior is specified in PEP 249, implementations of adapters aren’t always consistent with

forms.Widget.format_value()

format_value(value) Cleans and returns a value for use in the widget template. value isn’t guaranteed to be valid input, therefore subclass implementations should program defensively. Changed in Django 1.10: In older versions, this method is a private API named _format_value(). The old name will work until Django 2.0.

admin.ModelAdmin.get_changelist_form()

ModelAdmin.get_changelist_form(request, **kwargs) [source] Returns a ModelForm class for use in the Formset on the changelist page. To use a custom form, for example: from django import forms class MyForm(forms.ModelForm): pass class MyModelAdmin(admin.ModelAdmin): def get_changelist_form(self, request, **kwargs): return MyForm Note If you define the Meta.model attribute on a ModelForm, you must also define the Meta.fields attribute (or the Meta.exclude attribute). Howeve

auth.models.BaseUserManager.make_random_password()

make_random_password(length=10, allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789') Returns a random password with the given length and given string of allowed characters. Note that the default value of allowed_chars doesn’t contain letters that can cause user confusion, including: i, l, I, and 1 (lowercase letter i, lowercase letter L, uppercase letter i, and the number one) o, O, and 0 (lowercase letter o, uppercase letter o, and zero)

Custom Lookups

Django offers a wide variety of built-in lookups for filtering (for example, exact and icontains). This documentation explains how to write custom lookups and how to alter the working of existing lookups. For the API references of lookups, see the Lookup API reference. A simple lookup example Let’s start with a simple custom lookup. We will write a custom lookup ne which works opposite to exact. Author.objects.filter(name__ne='Jack') will translate to the SQL: "author"."name" <> 'Jack' T

gis.geoip2.GeoIP2.coords()

GeoIP2.coords(query) Returns a coordinate tuple of (longitude, latitude).

core.files.uploadedfile.UploadedFile.read()

UploadedFile.read() Read the entire uploaded data from the file. Be careful with this method: if the uploaded file is huge it can overwhelm your system if you try to read it into memory. You’ll probably want to use chunks() instead; see below.

GeoDjango

GeoDjango intends to be a world-class geographic Web framework. Its goal is to make it as easy as possible to build GIS Web applications and harness the power of spatially enabled data. GeoDjango TutorialIntroduction Setting Up Geographic Data Geographic Models Importing Spatial Data Spatial Queries Putting your data on the map GeoDjango InstallationOverview Requirements Installation Troubleshooting Platform-specific instructions GeoDjango Model APISpatial Field Types Spatial Field Options