Reputation: 84
I wrote one model that is having email field. I added some emails to it by admin. What I need is when I type any email that should check with my database email if exist it should show email is already there:
models.py #in my database already some data is there
class Friend(models.Model):
email = models.EmailField(max_length=100)
def __str__(self):
return self.email
forms.py
class FriendForm(forms.ModelForm):
class Meta:
model = Friend
fields = ['email']
views.py
def check(request):
form = FriendForm(request.POST or None)
if form.is_valid():
form = Friend.objects.filter(**form.cleaned_data)
context = {
'form': form
}
return render(request, "one.html", context)
I imported every thing. When I am trying this code it is directly rendering to one.html. What I need is it should check if email is there in database then only it should render.
Upvotes: 1
Views: 54
Reputation: 27513
class Friend(models.Model):
email = models.EmailField(max_length=100,unique=True,error_messages={'error':'The Email already exists'})
you can change your model to this.
if form.is_valid():
form = Friend.objects.filter(**form.cleaned_data)
else:
redirect('to_some_page')
Upvotes: 1