yourlabs / django-autocomplete-light

A fresh approach to autocomplete implementations, specially for Django. Status: v4 alpha, v3 stable, v2 & v1 deprecated.
https://django-autocomplete-light.readthedocs.io
MIT License
1.8k stars 468 forks source link

Help request to render autocompletes in template #645

Closed Pazitos10 closed 8 years ago

Pazitos10 commented 8 years ago

I'm trying to use django-autocomplete-light outside the admin, so I got this:

from dal import autocomplete
....
url(r'^something-autocomplete/$', my_app.views.SomethingAutocomplete.as_view(), name='something-autocomplete'),
...

in urls.py

Then, in forms.py:

...
from dal import autocomplete
...
class MyForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        ...

    class Meta:
        model = MyModel
        exclude = ['field']
        widgets= {
            'autocomplete-widget': autocomplete.ModelSelect2(url='something-autocomplete')
        }

Then in views.py

...
from dal import autocomplete
class SomethingAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        if not self.request.user.is_authenticated():
            return MyModel.objects.none()
        my_model = MyModel.objects.all()
        if self.q:
            my_model = my_model.filter(field__icontains=self.q)
        return my_model
...

def test_view(request):
    search_field = SomethingAutocomplete()
    form = ''
    context= {'something': form,'search_field': search_field}
    return render(request, 'Something/test_template.html',context)

And last, in test_template.html:

{% extends "base.html" %}
{% load staticfiles %}
{% load crispy_forms_tags %}

{% block welcome %}
{% endblock %}

{% block content %}
    <div class="form-test">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h4><strong>Test Form</strong></h4>
            </div>
            <div class="panel-body">
                <form action="" method="post">
                    {% csrf_token %}
                    {{ search_field.as_p }}
                    {% if something %}
                        <hr>
                        <h5 style="text-align: center"><strong>some data: </strong></h5><br>
                        {% if something.instance.creador == user %}
                            {% crispy something %}
                        {% else %}
                            <p>some message</p>
                        {% endif %}
                    {% endif %}
                <input type="submit" />
            </form>
            </div>
        </div>
    </div>
{% endblock %}

{% block scripts %}
    {{ block.super }}
    {{ search_field.media }}
{% endblock scripts %}

But when I go to my site, all I got is this:

form

And if I inspect the elements:

html-elements

Is nothing there, so I'm confused, what I'm doing wrong?

django version: 1.9 django-autocomplete-light version: 3.1.3

jpic commented 8 years ago

I don't think your Meta.widgets dict is configured correctly.

Shouldn't the key be the field name you want to specify the widget for ?

See: https://docs.djangoproject.com/en/1.9/topics/forms/modelforms/#overriding-the-default-fields

Pazitos10 commented 8 years ago

@jpic Yes! my bad, thanks for that!, I got this now:

...
from dal import autocomplete
...
class MyForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        ...

    class Meta:
        model = MyModel
        fields = ('field',)
        exclude = ['field-excluded']
        widgets = {
            'field': autocomplete.ModelSelect2(url='something-autocomplete')
        }

form

html-elements

jpic commented 8 years ago

Ooops, I think you should really try to make a form. You can't render a view object like that in a template !

def test_view(request):
    # That won't work
    search_field = SomethingAutocomplete()
    # Should be an instance of your form
    form = ''

Please, consider reading at least Django's documentation on forms:

https://docs.djangoproject.com/fr/1.9/topics/forms/

And perhaps even the example app:

https://github.com/yourlabs/django-autocomplete-light/tree/master/test_project/select2_outside_admin

Pazitos10 commented 8 years ago

Sorry again, I should updated that too:

def test_view(request):
    search_field = SomethingAutocomplete()
    form = MyForm()
    context= {'form': form,'search_field': search_field}
    return render(request, 'Something/test_template.html',context)
jpic commented 8 years ago

Remove search_fields.

Fix the template to render the form.

In doubt, refer to links posted above.

jpic commented 8 years ago

I'm warning you, if you don't read the documentation before commenting again i will have to close this, we can't do your homework :)

Pazitos10 commented 8 years ago

I solved it 10 mins ago with the example, thank you very much! ... Sorry again, wasn't my intention!

jpic commented 8 years ago

Thanks, I'm glad to know it works for you too ;)

jpic commented 8 years ago

Don't worry, you'll be in my situation a few years from now it will be your turn xD

And guess who was in your situation a few years ago ? hehe