0% found this document useful (0 votes)
14 views28 pages

m3 FSD

Uploaded by

arunabadiger01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views28 pages

m3 FSD

Uploaded by

arunabadiger01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

1.

Discuss in detail steps involved to activate the Admin Interface in Django

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.

Steps to Customize Change Lists

1. Define which fields to display


2. Add filters
3. Add search functionality
4. Add custom actions
5. Customizing the display of fields

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')

# Add search functionality


search_fields = ('title', 'author__name', 'isbn')

# Add custom actions


actions = ['mark_as_published']

# Custom display of fields


def title_uppercase(self, obj):
return obj.title.upper()
title_uppercase.short_description = 'Book Title'

list_display = ('title_uppercase', 'author', 'published_date', 'isbn')

# Custom admin action


def mark_as_published(self, request, queryset):
queryset.update(published_date='2023-01-01')
mark_as_published.short_description = 'Mark selected books as published on 2023-01-01'

admin.site.register(Book, BookAdmin)
b) Demonstrate the importance of creating a ModelAdmin class in Django.

7.Define the below: A] request.path, b] request.get_host() c] request.get_full_path() d]


request.is_secure() e] request.META f] HTTP_USER_AGENT g] REMOTE_ADDR
8.Illustrate with a example on list_display, list_filter, date_hierarchy, ordering, and fields
attributes in the BookAdmin class
list_display: This attribute specifies the fields to display in the change list page for
the Book model. You can include regular fields, callable methods, or related fields. For
example:
class BookAdmin(admin.ModelAdmin):
list_display = ['title', 'author', 'publication_date']
list_filter: Use this attribute to add filters to the right sidebar of the change list page.
It allows users to filter records based on specific fields. For instance:
class BookAdmin(admin.ModelAdmin):
list_filter = ['author', 'publication_date']
date_hierarchy: Set this attribute to a DateField or DateTimeField in your model. It
creates a date-based drilldown navigation on the change list page. Example:
class BookAdmin(admin.ModelAdmin):
date_hierarchy = 'publication_date'
ordering: Specify the default ordering for the records. You can use fields from the
model or callables defined in list_display. For example:
class BookAdmin(admin.ModelAdmin):
ordering = ['author', 'title']
fields: This attribute determines the order of fields in the edit form. It accepts a list of
field names. For instance:
class BookAdmin(admin.ModelAdmin):
fields = ['title', 'author', 'publication_date']
9. a) Discuss in detail on the Three Boolean Flags in User Permissions.

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}

response = requests.post(url, json=payload)

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

Create a form for adding a new book:

# 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

Map the view to a URL:

# books/urls.py
from django.urls import path
from .views import add_book

urlpatterns = [
path('add/', add_book, name='add_book'),
]
Template

Create a template for rendering the form:

<!-- books/add_book.html -->


<!DOCTYPE html>
<html>
<head>
<title>Add Book</title>
</head>
<body>
<h1>Add a New Book</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>
</body>
</html>

15. Create a simple Django project called urls_dispatcher_example with two


application(articles and Blog).
16. Illustrate with an example creating Model Forms
17. Discuss on the below in URL.conf A] Using Multiple View Prefixes B] Special-Casing URLs in
Debug Mode
18. Illustrate with an example on Passing Extra Options to View Functions
19. Illustrate with an example Faking Captured URLconf Values and Using Default View
Argument
20. Demonstrate with an example on Capturing Text in URLs by taking datetime.now as
example

21. Discuss in detail with a example on Including multiple URLconfs


22. Explain in detail about Include() with relevant examples

You might also like