Commits

tyrion  committed 3d55baa

Better syntax and improvements for ac_example.

  • Participants
  • Parent commits 4529601

Comments (0)

Files changed (2)

File ac_example/forms.py

 from django import forms
 from django.contrib.auth.models import User, Message
-import autocomplete as ac
+
+from autocomplete.widgets import AutoCompleteWidget
+from autocomplete.fields import ModelChoiceField
 
 class MessageForm(forms.ModelForm):
     class Meta:
         model = Message
 
-    user = ac.ModelChoiceField('user')
+    user = ModelChoiceField('user')
 
 class ExampleForm(forms.Form):
 
     # an existent user
-    an_existent_user = ac.ModelChoiceField('user')
+    an_existent_user = ModelChoiceField('user')
 
     # an existent username
-    an_existent_username = forms.CharField(widget=ac.AutoCompleteWidget('name'))
+    an_existent_username = forms.CharField(widget=AutoCompleteWidget('name'))
     # an username, either existent or not
-    an_username = forms.CharField(widget=ac.AutoCompleteWidget('name', False))
+    an_username = forms.CharField(
+        widget=AutoCompleteWidget('name', force_selection=False))

File ac_example/urls.py

 from django.conf.urls.defaults import *
+from django.conf import settings
+from django.contrib.auth.models import User
 
-from django.contrib.auth.models import User
 from autocomplete.views import autocomplete
 
 
-autocomplete.register('user', User.objects.all(), ('username', 'email'), 5)
-autocomplete.register('name', User.objects.all(), ('username',), 5, 'username', 'username')
+autocomplete.register(
+    id = 'user', 
+    queryset = User.objects.all(),
+    fields = ('username', 'email'),
+    limit = 5,
+)
+
+autocomplete.register(
+    id = 'name',
+    queryset = User.objects.all(),
+    fields = ('first_name',),
+    limit = 5,
+    key = 'username',
+    label = 'username',
+)
 
 urlpatterns = patterns('',
+    # If you want to use the same AutoComplete instance as a view for multiple
+    # applications, you should register it only once in your project's URLconf.
     url('^autocomplete/(\w+)/$', autocomplete, name='autocomplete'),
     url('^example/$', 'ac_example.views.example'),
 )
+
+# Remember to serve the files in autocomplete/media from your MEDIA_URL.
+# When DEBUG is True you can accomplish this by using the builtin django's
+# "static.serve" view. XXX Make sure that MEDIA_URL in your settings.py is
+# pointing to this view (i.e. "http://localhost:8000/site_media/").
+if settings.DEBUG:
+    urlpatterns += patterns('django.views.static',
+        (r'^site_media/(?P<path>.*)$', 'serve',
+            {'document_root': settings.MEDIA_ROOT}),
+    )