Skip to main content

A flexible Django package for adding search autocomplete functionality

Project description

Django Search Autocomplete

A flexible and easy-to-use Django package for adding search autocomplete functionality to your Django applications.

Author

Islam Elmasry
GitHub: emji555
Email: emji555@gmail.com

Features

  • Generic search functionality that works with any Django model
  • Configurable search fields and display fields
  • Support for foreign key relationships
  • Support for image and file fields
  • Customizable number of results
  • Bootstrap-styled UI out of the box
  • Easy integration with existing Django projects

Installation

  1. Install the package using pip:
pip install django-search-autocomplete
  1. Add 'search_autocomplete' to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
    ...
    'search_autocomplete',
]
  1. Include the URLs in your project's urls.py:
from django.urls import path, include

urlpatterns = [
    ...
    path('search-config/', include('search_autocomplete.urls')),
]

Usage

Method 1: Using the Configuration Interface

  1. Visit /search-config/config/ in your browser to access the configuration interface.
  2. Select the model you want to search
  3. Choose the fields to search in
  4. Select the fields to display in results
  5. Set the maximum number of results
  6. Copy the generated integration code

Method 2: Direct Implementation

  1. Add the required CSS and JavaScript to your template:
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  1. Add the search input to your template:
<div class="search-autocomplete">
    <input type="text" id="search-input" class="form-control" placeholder="Type to search...">
    <div id="search-results" class="list-group mt-2"></div>
</div>
  1. Initialize the search functionality:
const searchConfig = {
    model: 'app_name.model_name',
    searchFields: ['field1', 'field2'],
    displayFields: ['field1', 'field2'],
    maxResults: 5
};

let timeout = null;
$('#search-input').on('input', function() {
    clearTimeout(timeout);
    const query = $(this).val().trim();
    
    if (query.length < 2) {
        $('#search-results').empty().hide();
        return;
    }

    timeout = setTimeout(() => {
        $.get('/search-config/search/', {
            query: query,
            model: searchConfig.model,
            search_fields: searchConfig.searchFields,
            display_fields: searchConfig.displayFields,
            max_results: searchConfig.maxResults
        })
        .done(function(data) {
            const $results = $('#search-results').empty();
            
            data.results.forEach(function(result) {
                const $item = $('<div class="list-group-item">');
                searchConfig.displayFields.forEach(function(field) {
                    const value = result[field];
                    if (field.endsWith('__str')) {
                        $item.append($('<div>').text(value));
                    } else if (value && value.startsWith('/media/')) {
                        $item.append($('<img>').attr({
                            src: value,
                            alt: field,
                            style: 'max-width: 50px; max-height: 50px;'
                        }));
                    } else {
                        $item.append($('<div>').text(`${field}: ${value}`));
                    }
                });
                $results.append($item);
            });
            
            $results.show();
        });
    }, 300);
});

Configuration Options

  • model: The Django model in format 'app_name.model_name'
  • searchFields: List of fields to search in
  • displayFields: List of fields to display in results
  • maxResults: Maximum number of results to return (default: 5)

Special Field Types

  • Foreign Key Fields: Use field_name__str to display the string representation
  • Image/File Fields: Will automatically be displayed as images if the URL starts with '/media/'

Example

Here's a complete example using a Product model:

# models.py
from django.db import models

class Product(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()
    price = models.DecimalField(max_digits=10, decimal_places=2)
    image = models.ImageField(upload_to='products/')

    def __str__(self):
        return self.name
// template.html
const searchConfig = {
    model: 'myapp.Product',
    searchFields: ['name', 'description'],
    displayFields: ['name', 'price', 'image'],
    maxResults: 5
};

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_search_autocomplete-0.1.1.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_search_autocomplete-0.1.1-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file django_search_autocomplete-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django_search_autocomplete-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8e60f524323b56e23bb2000b41f2e2b405e5abe5494ddf09492a2c1bf32c4bee
MD5 6624bd26c5c6ff0d4b19c9cf64ac599c
BLAKE2b-256 527eaec7d4d812e9b9e4fa45cfe145b09ac2b21ed154161512b3481ea14f3378

See more details on using hashes here.

File details

Details for the file django_search_autocomplete-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_search_autocomplete-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1809fcc1d2117e23a84f1e0aa1422e973d9be77fe6da4b323270793d3861e776
MD5 63631762a110aa0452857dd257f55f67
BLAKE2b-256 957b301d4c8f2f5ce112d0889ac53b186894e127027146111659de2a0f5dfcbc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page