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 467 forks source link

Autocomplete with Django dev version (1.6) #65

Closed geaden closed 11 years ago

geaden commented 11 years ago

With project on Django 1.4.2 everything works fine, but when try to implement autocomplete in project on Django dev version a page with autocomplete widgets says every time "Autocomplete needs a url !" Static files included in template . URL to autocomplete presents in root url, and also in specific app urls. Also there is a problem in widget.js (// Add a class to ease css selection of autocompletes for widgets this.autocomplete.outerContainer.addClass - Uncaught TypeError)

jpic commented 11 years ago

Are you sure you're not talking about django 1.5 ? Will test with django 1.5

geaden commented 11 years ago

Currently I use Django 1.6.dev20121029060403 in my project (just git pull dev branch). I would appreciate if you could tell me how to solve that problem. Thank you.

jpic commented 11 years ago

I just tested test_project/fk_autocomplete with the current master on the Django git repo (1, 6, 0, 'alpha', 0) current git master the and couldn't reproduce your problem.

Could you show the code to reproduce your problem ?

Is it possible to reproduce with an example from test_project ?

Also, "Autocomplete needs a url" that usually means that the widget template has a problem, could you paste the generated widget html too ?

geaden commented 11 years ago

Oh, I didn't test it on test_project, just tried to implement it in my own project.

I have several apps (i.e. Project, Member)

In root directory I created autcomplete_light_registry.py with following code:

autocomplete_light.register(Project,
    search_fields=('name',),
    autocomplete_js_attributes={'placeholder': 'Project...'}
)

autocomplete_light.register(Member,
    search_fields=('name','),
    autocomplete_js_attributes={'placeholder': 'Member...'}
)

in root url conf (my_project.url) I put url(r'autocomplete/', include('autocomplete_light.urls')), and provide autocomplete_light.autodiscover() before admin.autodiscover()

In my forms (which are model forms) I use widgets = autocomplete_light.get_widgets_dict(Member)

And when I go to page with form I get an alert that "Autocomplete needs url !"

Generated widget html:

<div class="controls"><span class="autocomplete-light-widget project
    single" id="id_project-wrapper" data-max-values="1" data-bootstrap="normal">
<span id="id_project-deck" class="deck div"></span>
<input type="text" class="autocomplete" name="project-autocomplete" id="id_project_text" value="" autocomplete="off">
<select style="display:none" class="value-select" name="project" id="id_project" multiple="multiple"></select>
 <span style="display:none" class="remove div">         
X
    </span><span style="display:none" class="choice-template div"><span class="choice div">
    </span>
   </span>
  </span>
</div>

The problem is there's no data-autocomplete-url attribute, which presents in my own test project on Django 1.4.2 and it almost perfectly works (value of data-autocomplete-url="/autocomplete/MemberAutocompleteModelTemplate/")

And in widgets.js caught an error in line:

// Add a class to ease css selection of autocompletes for widgets
        this.autocomplete.outerContainer.addClass
jpic commented 11 years ago

I installed the code you gave me: https://github.com/jpic/django_test/tree/dal_issue_65 And I didn't see any problem.

The problem is likely to come from your code unfortunately ... I need to see the bug or I can't fix it, please try to isolate your bug with minimum code and post the code.

Maybe you have a problem in your custom form/widgets ?

The widget.js error is probably related to the other problem.

Thanks

jpic commented 11 years ago

Do you see your autocompletes on /autocomplete/ ?

Do you have a custom widget.hml template ?

Also, your code description looks ok, but you should paste actual code - stripped to the minimum.

Right now we have a bug but we don't know what code causes it in your project so it's very hard.

geaden commented 11 years ago

I tried to recreate mini version of my current app. You can find it here https://github.com/geaden/projectsapp. Thank you.

I don't have custom widget.html. But I am using autocomplete_html.

By the way I can access /autocomplete but access is denied (403 error). In app with working autocomplete I have the same issue.

jpic commented 11 years ago

Thanks, it seems like we're on the right track.

The problem is that the project doesn't run, there are many references to modules that aren't there (UnicodeNameMixin, nullable ...).

I did remove dead apps from INSTALLED_APPS like wishes, users, etc ...

Could you try to run your project in a fresh environment ?

Also, /autocomplete/ is only accessible by is_staff of course, else it would be a potential security problem heh ...

Thanks !

geaden commented 11 years ago

Sorry for bothering you. I've changed my app in order to be able to run. Please check https://github.com/geaden/projectsapp Thank you very much

jpic commented 11 years ago

Thanks, on what URL should I see the problem ? I had to add 'projectsapp' to INSTALLED_APPS and this:

--- a/projectsapp/templates/autocomplete_template/member_autocomplete.html
+++ b/projectsapp/templates/autocomplete_template/member_autocomplete.html
@@ -8,4 +8,4 @@
 {% else %}
     <p>No matches found</p>
 {% endif %}
-<a href="{% url member-add %}">Add member</a>
\ No newline at end of file
+<a href="{% url 'member-add' %}">Add member</a>

And I could use the autocomplete without any problem.

Could you fix this and reproduce the bug in the test project, and tell me step by step how to see the problem ? Thanks

Also, note that you can add an add another button outside the admin too.

geaden commented 11 years ago

Yeah, this projectsapp works correct. I don't know what's wrong in my production (it's a pity but I can't share whole project) Maybe it happens because i use bootstrap.js? Widgets work right, only autocomplete url parameter is not added(((

jpic commented 11 years ago

Keep on moving related stuff from your project into the test project. That's the standard "bug isolation" procedure and that's how we will figure out the problem.

A perhaps easier solution is to use ipdb. Put some import ipdb; ipdb.set_trace() in the widget at the point it sets the url and follow code from there, keeping an eye on the options dictionaries.

geaden commented 11 years ago

With bootstrap.js my test project works ok too. I'm going to use ipdb now. I will report as soon as get first results.

geaden commented 11 years ago

@jpic could you, please, tell where to locate ipdb.set_trace(). I have ho idea where to put it to correctly debug.

jpic commented 11 years ago

Before this: https://github.com/yourlabs/django-autocomplete-light/blob/master/autocomplete_light/widgets.py#L76

That's called by render() which is defined just after in the code.

render() renders the template with this template filter which should output data-autocomplete-url.

On Fri, Nov 23, 2012 at 5:48 PM, Gennady notifications@github.com wrote:

@jpic https://github.com/jpic could you, please, tell where to locate ipdb.set_trace(). I have ho idea where to put it to correctly debug.

— Reply to this email directly or view it on GitHubhttps://github.com/yourlabs/django-autocomplete-light/issues/65#issuecomment-10664678.

http://blog.yourlabs.org Customer is king - Le client est roi - El cliente es rey.

geaden commented 11 years ago

@jpic, sorry for wasting your time. I've at last fixed the problem. The problem was in using non unicode characters in placeholders attribute... I forgot to put 'u' before string. After I set it properly the problem has vanished. Thank you so much for diving me deep into Python and Python debugger as well. Thank you so much!

jpic commented 11 years ago

Well, you've learned the lesson about unicode strings the hard way ... like everybody :D

Cheers from Spain

On Fri, Nov 23, 2012 at 7:14 PM, Gennady notifications@github.com wrote:

@jpic https://github.com/jpic, sorry for wasting your time. I've at last fixed the problem. The problem was in using non unicode characters in placeholders attribute... I forgot to put 'u' before string. After I set it properly the problem has vanished. Thank you so much for diving me deep into Python and Python debugger as well. Thank you so much!

— Reply to this email directly or view it on GitHubhttps://github.com/yourlabs/django-autocomplete-light/issues/65#issuecomment-10666605.

http://blog.yourlabs.org Customer is king - Le client est roi - El cliente es rey.

geaden commented 11 years ago

Thanks again :)

Cheers from Russia