m3 FSD
m3 FSD
With that bit of configuration out of the way, now you can see the Django admin site in
action. Just run the development server (python manage.py runserver, as in previous chapters)
and visit http://127.0.0.1:8000/admin/ in your Web browser.
2. Demonstrate with a diagram how admin site is designed for non-technical users to develop
any web application.
3. Illustrate with an example how to add models to Admin Site and working of Admin Site
4. Illustrate with an example for Making fields optional in Django application
Once you’ve added that blank=True, reload the author edit form (http://127.0.0.1:
8000/admin/books/author/add/), and you’ll notice the field’s label—Email—is no longer
bolded. This signifies it’s not a required field. You can now add authors without needing to
provide e-mail addresses; you won’t get the loud red “This field is required” message
anymore if the field is submitted empty.
5. Illustrate with example how to customize field labels
6. a) Discuss with an example customizing change lists in a model in Django admin?
Customizing change lists in a Django admin model can enhance the usability and
presentation of data in the admin interface. Change lists are the lists of objects displayed in
the Django admin when you click on a model. You can customize these lists by defining
which fields to display, adding filters, search functionality, and custom actions.
Example
Let's walk through an example of customizing the change list for a Book model in the Django
admin.
# models.py
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
published_date = models.DateField()
isbn = models.CharField(max_length=13)
def __str__(self):
return self.title
Step 2: Register the Model with Custom Admin Options
Next, we will customize the admin interface for the Book model.
# admin.py
from django.contrib import admin
from .models import Book
class BookAdmin(admin.ModelAdmin):
# Define which fields to display in the change list
list_display = ('title', 'author', 'published_date', 'isbn')
# Add filters
list_filter = ('author', 'published_date')
admin.site.register(Book, BookAdmin)
b) Demonstrate the importance of creating a ModelAdmin class in Django.
b) From your perspective discuss when and when not to Use the Admin Interface
10. Demonstrate with an example GET and POST request.
GET Request Example: A GET request retrieves data from a server. You can use
the requests library in Python to make GET requests. Here’s an example:
import requests
url = "https://api.example.com/data"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
print("Data received successfully:", data)
else:
print("Error fetching data. Status code:", response.status_code)
POST Request Example: A POST request sends data to a server. You can include data in
the request body. Here’s an example using the same requests library:
import requests
url = "https://api.example.com/submit"
payload = {"name": "John Doe", "age": 30}
if response.status_code == 201:
print("Data submitted successfully!")
else:
print("Error submitting data. Status code:", response.status_code)
11. Illustrate with an example of a simple search form and its validation logic in detail.
12.Discuss on below Form Class a) Changing How Fields Are Rendered b) Setting a Maximum
Length
13. Discuss on below Form Class a) Setting Initial Values b) Adding Custom Validation Rules
14. Describe how you can combine the functionality of displaying a form and processing form
submissions within a single Django view function. Provide a code example and explain the
logic behind it
Form
# books/forms.py
from django import forms
from .models import Book
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = ['title', 'author', 'published_date']
View
Define a view that handles both displaying and processing the form:
# books/views.py
from django.shortcuts import render, redirect
from .forms import BookForm
def add_book(request):
if request.method == 'POST':
form = BookForm(request.POST)
if form.is_valid():
form.save()
return redirect('book_list') # Redirect to another view after successful submission
else:
form = BookForm()
return render(request, 'books/add_book.html', {'form': form})
URL
# books/urls.py
from django.urls import path
from .views import add_book
urlpatterns = [
path('add/', add_book, name='add_book'),
]
Template