Django form validation requires external resource

I have a Django form where the user enters a password. I can't validate the password locally; I call an external API to set the password, and if there's a problem it returns an error message (i.e. password too short, contains a dictionary word, etc.)

The API also needs the user's login name, which is not an element on that form, but is known in the view.

So where is the best place to do the form validation? In the form's clean() method, or in the view code?

And if I use the clean() method, how do I pass in the user's login name?

Thanks.

Answers


This validation is related to the form, and i think you should put this validation to the form. You can add username to form data and pass it to the form. For example,if username will be wrong, you can raise ValidationError. And others messages password short and etc. seems to be validation errors, so should be in the form.

For example you pass request.POST to the form. You can do something like this:

data = request.POST.dict()
data['username'] = 'myusername'
form = Form(data)

And what is the problem to add username to the form as field, if you always need it?


Need Your Help

Alternative / Similar to tag in UIView

iphone objective-c uiview tags

Is there any alternative to use like tag to property of UIView? The thing is I'd like to pass UITextField some NSInteger. Way to do is tag. But I want to pass 2 different NSInteger.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.