Admin Interface
JsWeb includes a built-in admin interface that allows you to manage your application's data. The admin interface is automatically generated based on your models and is ready for production use out of the box.
Table of Contents
- Enabling the Admin Interface
- Creating Admin Users
- Accessing the Admin Panel
- Managing Models
- Admin Features
- Security Considerations
- Customization
- Best Practices
Enabling the Admin Interface
To enable the admin interface, create an Admin instance and register your models with it:
# app.py
from jsweb import JsWebApp
from jsweb.admin import Admin
from .models import User, Post, Category
import config
app = JsWebApp(config=config)
# Create admin instance
admin = Admin(app)
# Register your models
admin.register(User)
admin.register(Post)
admin.register(Category)
This will create an admin interface accessible at /admin.
Auto-Generated Admin
The admin interface is completely auto-generated based on your models. No additional configuration needed!
Creating Admin Users
Before you can access the admin interface, you need to create an admin user:
This command will prompt you to enter: - Username: Your admin username - Email: Your admin email address - Password: A secure password
Creating Admin Users Programmatically
from .models import User
# Create admin user with code
admin_user = User.create(
username="admin",
email="admin@example.com",
password="secure_password",
is_admin=True
)
Security
Always use strong passwords. Never hardcode credentials in your application.
Accessing the Admin Panel
Once an admin user is created, you can access the admin panel:
- Navigate to
http://your-domain.com/admin - Log in with your admin credentials
- Manage your application's data
Managing Models
Viewing Records
The admin interface displays all records in a table format: - List View: See all records with key fields - Search: Find specific records - Filtering: Filter records by field values - Sorting: Sort by any column
Creating Records
Click the "Add" or "Create" button to add a new record. A form will appear with all model fields.
Editing Records
Click on any record in the list to edit its details. The edit form displays: - All model fields - Field values - Validation errors (if any)
Deleting Records
Click the delete button to remove a record. A confirmation will appear before deletion.
Admin Features
Automatic Field Detection
The admin interface automatically detects your model fields and displays appropriate widgets:
| Field Type | Widget |
|---|---|
| String | Text input |
| Integer | Number input |
| Float | Decimal input |
| Date | Date picker |
| DateTime | Date & time picker |
| Boolean | Checkbox |
| Text | Textarea |
| Foreign Key | Dropdown selector |
Search & Filter
The admin interface provides: - Full-text search across all fields - Field filtering by specific values - Boolean filtering for true/false fields - Relationship filtering for foreign keys
Bulk Actions
Perform operations on multiple records: - Select multiple records - Apply actions (delete, export, etc.)
Security Considerations
Admin Access Control
The admin interface should only be accessible to trusted administrators. Consider:
- Using strong passwords
- Enabling 2FA (if available)
- Restricting IP access with a proxy/firewall
- Using HTTPS in production
Data Protection
Be careful when deleting records. Deletions are usually permanent. Consider implementing:
Audit Logging
Consider adding audit logs to track admin actions:
Customization
Limiting Model Fields
Custom Model Lists
Future Customization
More customization options are planned for future versions, such as: - Custom column lists - Custom filters - Inline editing - Bulk operations
Best Practices
Model Organization
Keep your models organized in models.py:
Register Models Early
Register admin models right after creating the Admin instance:
Use Descriptive Names
Use clear, descriptive model names for the admin interface:
Validation in Models
Add validation to your models for better data quality: