2011-09-20 66 views
1

我在我的模型定義了3個URL字段如下:Django的條件網址verify_exists

image_1 = models.URLField(max_length=100, verify_exists=True, blank=True) 
image_2 = models.URLField(max_length=100, verify_exists=True, blank=True) 
image_3 = models.URLField(max_length=100, verify_exists=True, blank=True) 

我的問題是,有沒有辦法,我可以測試是否verify_exists回報TrueFalse如果是採取行動取決於在輸出?

回答

5

什麼Django的確實是用它的theURLValidator,看看網址是否有效。 你可能是使用相同的驗證這是目前在django.core

編輯: 比如可以說你要驗證是否Django的官方網站網址爲https://www.djangoproject.com/,存在與否。代碼將簡單地是這樣的:

from django.core.validators import URLValidator 
from django.core.exceptions import ValidationError 


my_url_validator = URLValidator(verify_exists=True) #creates a URLValidator object with verify_exists. 
my_url = "https://www.djangoproject.com/" #url to be verified 

#check if url is valid :) 
try:       
    my_url_validator(my_url) 
except ValidationError: 
    #not valid!! :_( 
    #fix: custom stuff to the rescue :)  
    CustomStuff()... 
+0

可能的輸出以改變字段值你請詳細說明你的意思是在django.core中使用相同的驗證,謝謝回覆:) – Paulo

+0

我已經詳細解釋了一下,希望能幫助 – Pannu

+0

謝謝,作品像一個魅力:D – Paulo