![](/img/trans.png)
[英]How do you write a regex in python that finds all word which contain only letters, numbers and underscore?
[英]Python / Django - How to create regex (RegexValidator) which allows only letters and spaces?
這是我的代碼:
class Location(models.Model):
alphaSpaces = RegexValidator(r'^[a-zA-Z]+$', 'Only letters and spaces are allowed in the Location Name.')
locationName = models.CharField(max_length=80, unique=True, validators=[alphaSpaces])
但是,當我嘗試創建一個名為“我的位置的位置”的位置時,會引發錯誤並顯示Only letters and spaces are allowed in the Location Name
”中Only letters and spaces are allowed in the Location Name
。
對於它的價值,我正在使用RESTful API,並在JSON對象中傳遞位置名稱,如下所示:
data = {'locationName': 'Location My Location'}
如果您還希望也允許使用空格,則您的正則表達式應為: '^[a-zA-Z ]+$'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.