python - How to search TextField for regular expression in Django? -
i submitting form in django , wrote clean function, attempts go through textfield , looks see if of words in field match words formatted --> #tweet, #people, , on, can not seem working(the form saves fine btw), knowledge of regular expression around zero, , yes know have used model form here experimenting because new django.
forms.py
class tweetform(forms.form): tweets = forms.charfield(widget=forms.textarea(), initial='write tweets here') class meta: fields=('tweets',) def clean(self): data = self.cleaned_data regular = re.compile('\b#\w\w+') clean_tweets = data['tweets'] lines in clean_tweets: words = regular.findall(lines) hashtags in words: print hashtags #want print them in terminal def __init__(self, userprofile, *args, **kwargs): super(tweetform, self).__init__(*args, **kwargs) self.userprofile = userprofile def save(self, *args, **kwargs): data=self.cleaned_data obj = tweet(tweets=data['tweets'], userprofile=self.userprofile, date=timezone.now(), ) obj.save()
i ended figuring out , how it, turned data in list , ran loop enumerator.
forms.py
def clean(self): data = self.cleaned_data regex = re.compile("\b#\w\w+") cleaned_tweets = data['tweets'] split_tweets = cleaned_tweets.split() i, x in enumerate(split_tweets): if re.search(regex, x): print i, x
Comments
Post a Comment