Mark Lavin avatar Mark Lavin committed aa4f3fd

Filling out example project.

Comments (0)

Files changed (9)

example/core/fixtures/initial_data.json

+[
+    {
+        "pk": 1, 
+        "model": "core.fruit", 
+        "fields": {
+            "name": "Apple"
+        }
+    }, 
+    {
+        "pk": 2, 
+        "model": "core.fruit", 
+        "fields": {
+            "name": "Pear"
+        }
+    }, 
+    {
+        "pk": 3, 
+        "model": "core.fruit", 
+        "fields": {
+            "name": "Grape"
+        }
+    }, 
+    {
+        "pk": 4, 
+        "model": "core.fruit", 
+        "fields": {
+            "name": "Orange"
+        }
+    }
+]

example/core/forms.py

+from django import forms
+
+import selectable.forms as selectable
+
+from example.core.lookups import FruitLookup
+
+
+class FruitForm(forms.Form):
+    autocomplete = forms.CharField(
+        label='Type the name of a fruit (AutoCompleteWidget)',
+        widget=selectable.AutoCompleteWidget(FruitLookup),
+        required=False,
+    )
+    newautocomplete = forms.CharField(
+        label='Type the name of a fruit (AutoCompleteWidget which allows new items)',
+        widget=selectable.AutoCompleteWidget(FruitLookup, allow_new=True),
+        required=False,
+    )
+    combobox = forms.CharField(
+        label='Type/select the name of a fruit (AutoComboboxWidget)',
+        widget=selectable.AutoComboboxWidget(FruitLookup),
+        required=False,
+    )
+    newcombobox = forms.CharField(
+        label='Type/select the name of a fruit (AutoComboboxWidget which allows new items)',
+        widget=selectable.AutoComboboxWidget(FruitLookup, allow_new=True),
+        required=False,
+    )
+    autocompleteselect = selectable.AutoCompleteSelectField(
+        lookup_class=FruitLookup,
+        label='Select a fruit (AutoCompleteField)',
+        required=False,
+    )
+    newautocompleteselect = selectable.AutoCompleteSelectField(
+        lookup_class=FruitLookup,
+        allow_new=True,
+        label='Select a fruit (AutoCompleteField which allows new items)',
+        required=False,
+    )
+    comboboxselect = selectable.AutoComboboxSelectField(
+        lookup_class=FruitLookup,
+        label='Select a fruit (AutoComboboxSelectField)',
+        required=False,
+    )
+    newcomboboxselect = selectable.AutoComboboxSelectField(
+        lookup_class=FruitLookup,
+        allow_new=True,
+        label='Select a fruit (AutoComboboxSelectField which allows new items)',
+        required=False,
+    )
+    multiautocompleteselect = selectable.AutoCompleteSelectMultipleField(
+        lookup_class=FruitLookup,
+        label='Select a fruit (AutoCompleteSelectMultipleField)',
+        required=False,
+    )
+    multicomboboxselect = selectable.AutoComboboxSelectMultipleField(
+        lookup_class=FruitLookup,
+        label='Select a fruit (AutoComboboxSelectMultipleField)',
+        required=False,
+    )
+

example/core/lookups.py

+from selectable.base import ModelLookup
+from selectable.registry import registry
+
+from example.core.models import Fruit
+
+
+class FruitLookup(ModelLookup):
+    model = Fruit
+    search_field = 'name__icontains'
+
+
+registry.register(FruitLookup)
+

example/core/models.py

+from django.contrib import admin
 from django.db import models
 
-# Create your models here.
+
+class Fruit(models.Model):
+    name = models.CharField(max_length=200)
+
+    def __unicode__(self):
+        return self.name
+
+
+admin.site.register(Fruit)

example/core/templates/base.html

+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
+	<title>Django-Selectable Example</title>
+        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/themes/base/jquery-ui.css" type="text/css" media="screen">
+        <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/themes/ui-lightness/jquery-ui.css" type="text/css" media="screen">
+        <link rel="stylesheet" href="{{ STATIC_URL }}css/dj.selectable.css" type="text/css" media="screen">
+        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
+        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js"></script>
+        <script type="text/javascript" src="{{ STATIC_URL }}js/jquery.dj.selectable.js"></script>
+    </head>
+    <body>
+        <form action="{% url example-index %}" method="post" >
+            {% csrf_token %}
+            {% if form.is_valid %}
+                {{ form.as_p }}
+                <h2>Cleaned Data</h2>
+                {{ form.cleaned_data }}
+            {% else %}
+                {{ form.as_p }}
+            {% endif %}
+            <button>Submit</button>
+        </form>
+    </body>
+</html>

example/core/urls.py

+from django.conf.urls.defaults import *
+
+
+urlpatterns = patterns('example.core.views',
+    url(r'^', 'index', name='example-index'),
+)

example/core/views.py

-# Create your views here.
+from django.shortcuts import render_to_response
+from django.template import RequestContext
+
+from example.core.forms import FruitForm
+
+
+def index(request):
+
+    if request.method == 'POST':
+        form = FruitForm(request.POST)
+    else:
+        form = FruitForm()
+
+    return render_to_response('base.html', {'form': form}, context_instance=RequestContext(request))

example/settings.py

 #     'django.template.loaders.eggs.Loader',
 )
 
+TEMPLATE_CONTEXT_PROCESSORS = (
+    "django.contrib.auth.context_processors.auth",
+    "django.core.context_processors.debug",
+    "django.core.context_processors.i18n",
+    "django.core.context_processors.media",
+    "django.core.context_processors.request",
+    "django.contrib.messages.context_processors.messages",
+    "staticfiles.context_processors.static",
+)
+
 MIDDLEWARE_CLASSES = (
     'django.middleware.common.CommonMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.contrib.sessions',
     'django.contrib.sites',
     'django.contrib.messages',
-    # Uncomment the next line to enable the admin:
-    # 'django.contrib.admin',
-    # Uncomment the next line to enable admin documentation:
-    # 'django.contrib.admindocs',
+    'django.contrib.admin',
+
     'staticfiles',
     'selectable',
     'example.core',
 from django.conf.urls.defaults import *
 
+from staticfiles.urls import staticfiles_urlpatterns
+
 # Uncomment the next two lines to enable the admin:
-# from django.contrib import admin
-# admin.autodiscover()
+from django.contrib import admin
+admin.autodiscover()
 
 urlpatterns = patterns('',
-    (r'^example/', include('example.core.urls')),
-
     # Uncomment the admin/doc line below to enable admin documentation:
     # (r'^admin/doc/', include('django.contrib.admindocs.urls')),
 
     # Uncomment the next line to enable the admin:
-    # (r'^admin/', include(admin.site.urls)),
+    (r'^admin/', include(admin.site.urls)),
+    (r'^_/', include('selectable.urls')),    
+    (r'^', include('example.core.urls')),    
 )
+
+urlpatterns += staticfiles_urlpatterns()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.