我想檢查添加的用戶輸入是否存在於數據庫中。如果它將用戶重定向到現有對象絕對URL,則通過並繼續保存輸入。到目前爲止,我設法檢查存在,並獲得對象,但返回HttpResponseRedirect(input_get.get_absolute_url())
不重定向,雖然我可以在終端(/tags/215/baseball/
)正確打印URL。 而是我得到:django如果對象存在重定向到絕對url
「確保此值至多有50個字符(它有65)」。
有什麼建議嗎?感謝
forms.py
class TagsForm(forms.ModelForm):
def clean_tag_word(self):
tag_word = self.cleaned_data['tag_word']
tag_filter = TagsNew.objects.filter(tag_word=tag_word)
if tag_filter.exists():
tag_get = TagsNew.objects.get(tag_word=tag_word)
tag_redirect=tag_get.get_absolute_url()
print (tag_redirect)
return HttpResponseRedirect(tag_redirect)
else:
pass
return tag_word
models.py
(get_absolute_url):
def get_absolute_url(self):
return reverse('tags:tagsnew_detail', kwargs={'pk':self.pk, 'slug':self.slug})
models.py:
class TagsNew(models.Model):
id = models.AutoField (primary_key=True)
tag_word = models.CharField(max_length=50, blank=True)
slug = models.CharField(max_length=50, blank=True)
timestamp=models.DateTimeField(auto_now_add=True, auto_now= False)
updated=models.DateTimeField(auto_now_add=False, auto_now= True)
class Meta:
managed = True
db_table = 'tags_new'
def save(self, *args, **kwargs):
self.slug = slugify(self.tag_word)
super(TagsNew, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('tags:tagsnew_detail', kwargs={'pk':self.pk, 'slug':self.slug})
def __str__(self):
return self.tag_word
跟蹤:
/tags/221/corrections/
<ul class="errorlist"><li>tag_word<ul class="errorlist"><li>Ensure this value has at most 50 characters (it has 65).</li></ul></li></ul>
[08/Apr/2015 04:47:52] "POST /tags/add/ HTTP/1.1" 200 16662
[08/Apr/2015 04:47:52] "GET /static/js/ajax.js HTTP/1.1" 404 1634
[08/Apr/2015 04:47:52] "GET /static/tags/css/style.css HTTP/1.1" 304 0
你能不能請示TagsNew模型?你似乎已經提到了在某些領域的最大長度爲50。 –
當然,我只是加了模型 – Torostar
也請添加完整的跟蹤。謝謝 –