开发者

Validating URL paramaters

How should one validate URL parameters in a view? Would this be accomplished using a a bunch of ìf statements or is there a better way to go about it? I'd like to validate the parameters when the request comes in rather than having it scattered across my view, model and manager.

When I say validation I'm referring to basic checks such as existence of a key, checking the data type,开发者_如何学JAVA integer ranges, etc.

Thanks.


I think in this case it depends on the scale of your application if it's just a small application doing data validation via simple if statements would be the easiest route, but django does have features to support nicer form validation(https://docs.djangoproject.com/en/1.3/ref/forms/validation/), so it might be cleaner to do it that way, but it still boils down to if statements in the end.


Your urls.py can take care of some of the validation with an appropriate regex (e.g., use \d for integer values instead of \w) but existence of a key or anything more sophisticated has to do be done in your view code itself, typically with if checks.

Key existence validation logic can be simplified with django.shortcuts.get_object_or_404 or using a base view class or decorator though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜