question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DAL v3: select form field is populated with "--------"

See original GitHub issue

Hello, I am trying to implement a simple autocomplete with a select form field using the User object model. My select field is empty apart from the dashes and I can’t enter anything in it. I fail to link/associate my UsernameForm to the login.html correctly. It should at least populate the select form field. I could really use some pointers to move forward. Thank you for your time!

This is what I have so far:

the localhost/type_ahead/?q

{"pagination": {"more": false}, "results": [{"text": "testuser1", "id": 1}, {"text": "testuser2", "id": 2}]}

urls.py

from dal import autocomplete
urlpatterns = [
    url(
        r'^type_ahead/$',
        TypeAheadUsers.as_view(),
        name='list_users',
    ),
]

views.py

from dal import autocomplete
class TypeAheadUsers(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        qs = User.objects.filter(is_active=True).order_by("id")
        if self.q:            
            qs = qs.filter(username__istartswith=self.q)
        return qs

models.py

from dal import autocomplete
class UsernameForm(ModelForm):
    username = forms.ModelChoiceField(
        queryset=User.objects.filter(is_active=True).order_by("id"),
        widget=autocomplete.ModelSelect2(url='list_users'),
    )    
    class Meta:
        model = User
        fields = ('username',)

views.py

a login method that passes a context dictionary c to the login.html file

def login(request,invalid_login=None, username=None):
    c = {}
    c.update(csrf(request))

    if(invalid_login):
        c.update({"invalid_login":True})
    else:
        c.update({"invalid_login":False})

    c.update({"username": username if username else None})

    try:
        tmp = UsernameForm()
        logger.debug("show username field: {0}".format(tmp))
        c.update({"ta_username": tmp})
    except Exception as e:
        logException(e)
    return render(request, 'login.html', c)

login.html

{% extends "base.html" %}

{% block content %}
<div class="login_main">
  {% load staticfiles %}
  <div class="login_productname2">Super Awesome Product Name Here</div>
  <div id="login_sec" class="login_numpad">

    <form action="/account/auth/" method="post" id="pinForm">
      {% csrf_token %}
      <table class="LoginAccount"> 
        <tr>          
          <td>Username:<span id="userRefresh" class="Refresh" onclick="refreshLoginUserList()"></span></td>
        </tr>
        <tr>
          <td>
            {{ta_username.username}}
          </td>
        </tr>
        <tr>
          <td><label for="passwd">PIN:</label></td>
        </tr>
        <tr>
          <td><input type="password" class="selected" maxLength="4" name="passwd" value="" id="passwd">{% if invalid_login %}<span class="ErrorMsg">Incorrect password</span>{% endif %}</td>
        </tr>
        <tr>
          <td height="1pt" colspan="1">&nbsp;</td>
        </tr>
      </table>
      {% include "NumberPad.html" %}      
    </form>
    <script type="text/javascript" src="{% static 'scripts/jquery.js' %}"></script>
    <div class="login_productname3">Rev. {{ epcRev }}</div>
  </div>
</div>

{% endblock content %}

the rendered select form field

<select data-autocomplete-light-function="select2" data-autocomplete-light-url="/type_ahead/" id="id_username" name="username">
    <option value="" selected="selected">---------</option>
</select>

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jpiccommented, Mar 22, 2016

Perhaps this will help, it helped a lot of SO users: blog.yourlabs.org/post/30382323418/surviving-djangocontribstaticfiles-or-how-to

1reaction
thainlnguyencommented, Mar 22, 2016

obviously, i am not very good at following instructions. i don’t know why i miss the {{form.media}} field, but after putting it in everything is fine. there were some js errors but that was because i didn’t copy the static files to my project tree.

it works and thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

The database abstraction layer - Web2py
web2py comes with a Database Abstraction Layer (DAL), an API that maps Python objects into database objects such as queries, tables, and records....
Read more >
Tutorial - django-autocomplete-light 3.9.0rc1 documentation
In order to forward field value DAL searches for the field considering form prefixes and then decides how to forward it to the...
Read more >
Best Way to Populate Form Fields From Select Field
So the trick is to have a event listener in your select menu, which is executed when your cat is changed. Afterwards, it...
Read more >
How to Implement Dependent/Chained Dropdown List with ...
Dependent or chained dropdown list is a special field that relies on a previously selected field so to display a list of filtered...
Read more >
Fields not populated in form connected to SQL tabl...
Thank you. When they didn't populate automatically, I manually added them after connecting to the SQL datasource, but after I added the 3...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found