有一種方法如下面提到
def content_file_name(instance, filename):
import random
import string
# random_str length will be 16 which will be combination of (4 digits + 4 characters + 4 digits + 4 characters)
random_str = "".join([random.choice(string.uppercase) for i in range(0,4)]) + "".join([random.choice(string.digits) for i in range(0,4)]) + \
"".join([random.choice(string.lowercase) for i in range(0,4)]) + "".join([random.choice(string.digits) for i in range(0,4)])
# return string seperated by hyphen eg:
random_str = random_str[:4] + "-" + random_str[4:8] + "-" + random_str[8:12] + "-" + random_str[12:]
filetype = filename.split(".")[-1].lower()
filename = random_str +"." + filetype
path = "yourapp/uploads/" + str(datetime.datetime.now().year) + "/" + str(datetime.datetime.now().month) + "/" + str(datetime.datetime.now().day) + "/"
os_path = os.path.join(path, filename)
return os_path
現在添加上述方法成其具有附接 EX模型字段: -
WagtailForm.save() 將保存數據。
哦!這太棒了! 但最後一個問題。如何將此字段實現爲模板形式? –
您不需要在表單中做任何事 –
但是如何在WagtailAdmin中顯示錶單提交? –