I made a text input filed with jquery autocomplete where I query the users. So if I start typing the name of a user it shows the related users. It works fine but I like to avoid that if the user like not to choose from the pupped up list and possibly type all the name and it has a mistake. So I like to make a function that checks if the added value of the field is equals with any of the users in the database. How to do that?
html
<input type="text" class="form-control" placeholder="Type name here" name="kap_bar_01" id="kap_bar_01">
<script>
$(function() {
var names = [
{% for u in related_users %}
"{{ u.user.last_name }} {{ u.user.first_name }}",
{% endfor %}
];
$( "#kap_bar_01" ).autocomplete({
source: names
});
});
</script>
models.py
class Kapcsolodasok(models.Model):
def __str__(self):
return str(self.user_name)
user_name = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
date = models.DateField(auto_now_add=True, auto_now=False, blank=True)
kap_bar_01 = models.TextField(max_length=200)
views.py
def kapcsolodasok(request):
profile = Profile.objects.get(user=request.user)
related_users = Profile.objects.filter(projekt=profile.projekt)
context = {
'related_users': related_users,
'profile': profile,
}
#lots of stuffs here
return render(request, 'stressz/kapcsolodasok.html', context)
Thank you in advance!
from django.contrib.auth.models import User
userList = User.objects.values()
The above code will get the usernames as list and loopover to check the username entered.But i think you want it to be checked in realtime so you have to use channels or ajax etc.Otherwise just pass the username value as url url/ and check it