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
- Install the package using pip:
pip install django-search-autocomplete
- Add 'search_autocomplete' to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
...
'search_autocomplete',
]
- 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
- Visit
/search-config/config/in your browser to access the configuration interface. - Select the model you want to search
- Choose the fields to search in
- Select the fields to display in results
- Set the maximum number of results
- Copy the generated integration code
Method 2: Direct Implementation
- 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>
- 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>
- 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 indisplayFields: List of fields to display in resultsmaxResults: Maximum number of results to return (default: 5)
Special Field Types
- Foreign Key Fields: Use
field_name__strto 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_search_autocomplete-0.1.0.tar.gz.
File metadata
- Download URL: django_search_autocomplete-0.1.0.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
45a2b82de6b031e5f147e274579a3288bc5f699c425ec58ed23fd6e725b8aa01
|
|
| MD5 |
56322813b3f7d1b9b03f42ed27993250
|
|
| BLAKE2b-256 |
bd0de4f96e79dd8a01014571c5ba93a76d56c254c0fa0b517ccd585aeebf5b39
|
File details
Details for the file django_search_autocomplete-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_search_autocomplete-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c5ac8c91906e69dc580ef4ed804a39c00dd348264acc5a6118b043410638edda
|
|
| MD5 |
c599c837c481565a7f09f69f79e28a55
|
|
| BLAKE2b-256 |
20d38392ee973b583c0e1d3bb6cf7fe22acc8c42f3ac2736fefc51d5b90dc1dc
|