簡體   English   中英

Python / Django-如何創建僅允許字母和空格的正則表達式(RegexValidator)?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM