Cleaning form data in Django

How can i clean and modify data from a form in django. I would like to define it on a per field basis for each model, much like using ModelForms.

What I want to achieve is automatically remove leading and trailing spaces from defined fields, or turn a title (from one field) into a slug (which would be another field).


Asked by: Adelaide322 | Posted: 24-09-2021






Answer 1

You can define clean_FIELD_NAME() methods which can validate and alter data, as documented here: http://docs.djangoproject.com/en/dev/ref/forms/validation/#ref-forms-validation

Answered by: Michelle171 | Posted: 25-10-2021



Similar questions

python - Cleaning an image to only black

I have an image. I would like to go over that image, pixel by pixel, and any pixel that is not black should be turned to white. How do I do this? (Python). Thanks!


python - cleaning my list

Hello I have a loop which goes counts different records in my MySQL database and then saves the numbers to a list. Here is the list: [1L, 2L, 2L, 5L, 4L, 1L, 1L, 1L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 2L, 4L, 2L, 1L, 3L, 1L, 2L, 4L, 1L, 2L, 1L, 1L, 3L, 1L, 3L, 1L, 5L, 2L, 1L, 1L, 5L, 1L, 1L, 1L, 4L, 2L, 1L, 3L, 2L, 1L, 2L, 2L, 2L, 3L, 1L, 1L, 3L, 2L, 2L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L...


python - Fixing and cleaning form input in Django

Presumably the clean_ methods in Django are designed to allow inline cleaning of input form data as well as accepting/rejecting submissions. Given a model where "FooBar" is valid input. Is there a standard way of fixing things like "FOOBAR" "foo-bar" "FooBar " in a somewhat fuzzy and generalizable manner before validation?


regex - Python String Cleaning

I am writing a program in PyQT that needs to take messy strings and clean them up. The possible input values are extremely variable. For example I would like to take the strings: "Seven_Pounds_(BDrip_1080p_ENG-ITA-GER)_Multisub_x264_bluray_.mkv", "The_Birds_1963_HDTV_XvidHD_720p-NPW.avi", "1892.XVID.AC3.HD.120_min.avi" and turn them into: "Seven Pounds", "The Birds"...


python - Cleaning up in pypy

I've been looking for ways to clean up objects in python. I'm currently using pypy. I found a web page and an example. First a basic example: class FooType(object): def __init__(self, id): self.id = id print self.id, 'born' def __del__(self): print self.id, 'died' ft = FooType(1) This SHOULD print: 1 born 1 died BUT it just...


Python regex - cleaning some Gcode

Ive been battling with this for a while and would appreciate a tip! I'm stripping certain elements from a Gcode file before sending it to a 3d printer My string is f100G00X345.234Y234F345.5623I-2344.234J234.234F544 or similar and I watch to match (with a view to removing) the elements where the letter 'F|f' is followed by a number (float), in the string above these are: ...


Perl to Python - CSV cleaning

I'm forced to turn from Perl to Python now, took a course even. But I'm struggling already with a simple data cleaning task on CSV files. I want to replace a ¶ by a SPACE in a specific column, the other columns must be untouched: In Perl this is very straight forward and works like a charm: perl -F";" -lane 'BEGIN {$,=";"} print $F[0],$F[1],$F[2],$F[3],$F[4],$F[5],$F[6],$F[7],$F[8],$F[9],$F[10],$F[1...


python - Cleaning HTML tables with Pandas

I would like to read a table on a website and parse the values. For this I did the following: url = 'http://www.astro.keele.ac.uk/jkt/debcat/' df = pd.read_html(url, header=0) Even with header=0, I am still left with a header with is df[0], so I do the following: df = df[1] df1.shape (161, 11) df1.columns Index([u' System ', u' Period (days) ', u' V B-V ', u' Spectra...


python - Cleaning up text in UTF-8

I have an interface where users can enter human-readable names in one line of text for different types of entities, mostly referring to business-related tasks, activities and operations. This includes numbers as well. We want this interface to support multiple human languages, but at the same time we would like to avoid entries that could break things later, specifically formatting, new lin...


Cleaning HTML using python

I have the below code however i am receiving an error. I am trying to get the text from an html file between Tag1 and Tag2 without the for loop the code is working (for one file) however when looping in a directory it is not from bs4 import BeautifulSoup from urllib import urlopen import os import bleach import re rootdir = mydirectory for subdir, dirs, files in os.walk(rootdir): for file in files: ...


python - Cleaning up a database in django before every test method

By default when Django runs against sqlite backend it creates a new in memory database for a test. That means for every class that derives from unittest.TestCase, I get a new database. Can this be changed so that it is cleared before every test method is run? Example: I am testing a manager class that provides additional abstraction on top of Django persistent objects. The code looks more-less like that


python - Cleaning an image to only black

I have an image. I would like to go over that image, pixel by pixel, and any pixel that is not black should be turned to white. How do I do this? (Python). Thanks!


Cleaning up nested for loops in python

I have this code: def GetSteamAccts(): #Get list of steam logins on this computer. templist = [] Steamapp_Folders = ["C:\\Program Files (x86)\\Steam\\steamapps\\", "C:\\Program Files\\Steam\\steamapps\\"] #Check both of these directories. for SF_i in range(len(Steamapp_Folders)): if os.path.exists(Steamapp_Folders[SF_i]): #If the directory even exists... Steam_AppDir_Items = ...


python - Cleaning data which is of type URLField

I have a simple URLField in my model link = models.URLField(verify_exists = False, max_length = 225) I would like to strip the leading and trailing spaces from the field. I don't think I can do this in "clean_fieldname" or in the "clean" method. Do I need to sub-class the "URLField" and remove the spaces in to_python method? Is there a better way to do this without any sub-classin...


python: cleaning up a string

i have a string like this somestring='in this/ string / i have many. interesting.occurrences of {different chars} that need to .be removed ' here is the result i want: somestring='in this string i have many interesting occurrences of different chars that need to be removed' i started to manually do all kinds of .replace, but there are so ...


python - cleaning my list

Hello I have a loop which goes counts different records in my MySQL database and then saves the numbers to a list. Here is the list: [1L, 2L, 2L, 5L, 4L, 1L, 1L, 1L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 2L, 4L, 2L, 1L, 3L, 1L, 2L, 4L, 1L, 2L, 1L, 1L, 3L, 1L, 3L, 1L, 5L, 2L, 1L, 1L, 5L, 1L, 1L, 1L, 4L, 2L, 1L, 3L, 2L, 1L, 2L, 2L, 2L, 3L, 1L, 1L, 3L, 2L, 2L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L...


python - Fixing and cleaning form input in Django

Presumably the clean_ methods in Django are designed to allow inline cleaning of input form data as well as accepting/rejecting submissions. Given a model where "FooBar" is valid input. Is there a standard way of fixing things like "FOOBAR" "foo-bar" "FooBar " in a somewhat fuzzy and generalizable manner before validation?


python - Cleaning memory after loading data from a JSON

I am loading a JSON file to parse it and convert it (only a part of the JSON) to a CSV. So at the end of the method I would free the space of the loaded JSON. Here is the method: def JSONtoCSV(input,output): outputWriter = csv.writer(open(output,'wb'), delimiter=',') jsonfile = open(input).read() data = loads(jsonfile) for k,v in data["specialKey"].iteritems(): outputWriter.writer...


r - Python or awk/sed for cleaning data


regex - Python String Cleaning

I am writing a program in PyQT that needs to take messy strings and clean them up. The possible input values are extremely variable. For example I would like to take the strings: "Seven_Pounds_(BDrip_1080p_ENG-ITA-GER)_Multisub_x264_bluray_.mkv", "The_Birds_1963_HDTV_XvidHD_720p-NPW.avi", "1892.XVID.AC3.HD.120_min.avi" and turn them into: "Seven Pounds", "The Birds"...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top