Below is a basic example of how you could structure the urls.py, views.py, models.py, and a template file (let's assume notification_list.html) for a Django app named notifications that displays a list of notifications. This example assumes you have a basic understanding of Django and have already set up a Django project with the notifications app created and installed in your INSTALLED_APPS setting.
1. models.py (Define your Notification model)
python
# notifications/models.py
from django.db import models
from django.utils import timezone
# Define a model for notifications
class Notification(models.Model):
# Choices for notification types
MESSAGE_TYPES = [
('info', 'Info'),
('warning', 'Warning'),
('error', 'Error'),
('success', 'Success'),
]
title = models.CharField(max_length=255) # Title of the notification
message = models.TextField() # Detailed message of the notification
type = models.CharField(max_length=10, choices=MESSAGE_TYPES, default='info') # Type of notification
created_at = models.DateTimeField(default=timezone.now) # Timestamp for when the notification was created
# String representation of the model for better readability in the admin interface
def __str__(self):
return self.title
2. admin.py (Register the model with the admin interface)
python
# notifications/admin.py
from django.contrib import admin
from .models import Notification
# Register the Notification model with the admin interface for easy management
admin.site.register(Notification)
3. views.py (View to handle the notification list)
python
# notifications/views.py
from django.shortcuts import render
from .models import Notification
# View function to display a list of notifications
def notification_list_view(request):
# Fetch all notifications from the database, ordered by the most recent first
notifications = Notification.objects.all().order_by('-created_at')
# Context dictionary to pass data to the template
context = {
'notifications': notifications,
}
# Render the notification_list.html template with the context
return render(request, 'notifications/notification_list.html', context)
4. urls.py (URL pattern for the notification list view)
python
# notifications/urls.py
from django.urls import path
from .views import notification_list_view
# Namespace for the app's URLs
app_name = 'notifications'
# URL pattern for the notification list view
urlpatterns = [
path('', notification_list_view, name='list'),
]
Note: If you haven't already, you'll also need to include the notifications app's URL configuration in your project's main urls.py file.
python
# project/urls.py (assuming your project is named 'project')
from django.contrib import admin
from django.urls import include, path
# Main URL configuration for the project
urlpatterns = [
path('admin/', admin.site.urls),
path('notifications/', include('notifications.urls')), # Include the app's URL config
]
5. notification_list.html (Template to display the list of notifications)
Create a new directory named templates inside your notifications app directory if it doesn't already exist. Within templates, create another directory named notifications (this is a common Django naming convention for template directories to match the app name), and inside that, create your notification_list.html file.
In your settings.py, make sure STATIC_URL is configured:
python
STATIC_URL = 'static/'
In your main urls.py, add the following to serve static files during development:
python
from django.conf import settings
from django.conf.urls.static import static
# ... other URL patterns ...
# Serve static files during development (not for production)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Final Steps:
Make migrations and migrate: Run python manage.py makemigrations followed by python manage.py migrate in your terminal from the project root to apply the model changes to your database.
Create some notifications:
Using the Django Admin Interface:
Ensure you've registered the Notification model in admin.py as shown above.
Run python manage.py runserver to start the development server.
Navigate to http://localhost:8000/admin/, log in, and click on the Notifications model to create new instances.
Using the Django Shell:
Open a terminal and run python manage.py shell.
Import the model: from notifications.models import Notification.
Create a notification: Notification.objects.create(title='Test', message='This is a test notification.', type='info').
View your notifications list: Navigate to http://localhost:8000/notifications/ (assuming your development server is running on the default port) to see the list of notifications.