Autocomplete with username and image

我们两清 提交于 2020-06-17 09:34:07

问题


I am using autocomplete to dropdown username and user profile picture, i am getting the username correctly but profile picture is showing default images instead of the user profile picture. I do not know if i am passing this right in jquery. Here is what i have tried:

Model:

class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,blank=True,null=True)
    profile_pic = models.ImageField(upload_to='ProfilePicture/', default="ProfilePicture/user-img.png", blank=True)

Views:

def home(request):
    all_users = User.objects.values_list('username', flat=True)
    all_img = Profile.objects.all().distinct()
context = {
    'all_users': all_users,
    'all_img': all_img,
}

Template:

<div class="ui-widget"> 
 <input type="text" name="" class="autocomplete" >
</div>

jQuery:

<script type="text/javascript">
//TAG AUTOCOMPLETE FORM DROPDOWN USER NAMES
$(function() {
  var availableTags = [
      {% for user in all_users %}
          "{{user|safe}}",
      {% endfor %}
  ];

  $(".autocomplete").autocomplete({
    source: availableTags,

}).data("ui-autocomplete")._renderItem = function (ul, item) {
 var image = "<div>{% for img in all_img %}{% if img.profile_pic %}<img src='{{ img.profile_pic.url 
}}'{% endif %}{% endfor %}" + " height='55' /> " + item.label + "</div>";

return $("<li></li>")
    .data("item.autocomplete", item)
    .append(image)
    .appendTo(ul);
};
});
</script>

来源:https://stackoverflow.com/questions/62356707/autocomplete-with-username-and-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!