0% found this document useful (0 votes)
4 views

django admin interface

The document covers various aspects of Django, including admin interfaces, model forms, and generic views. It explains how to implement search functionality, handle form processing, and utilize generic views for common tasks like displaying lists and details. Additionally, it discusses extending generic views and exporting data in formats like CSV and PDF for reporting and sharing purposes.

Uploaded by

brsmce007
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

django admin interface

The document covers various aspects of Django, including admin interfaces, model forms, and generic views. It explains how to implement search functionality, handle form processing, and utilize generic views for common tasks like displaying lists and details. Additionally, it discusses extending generic views and exporting data in formats like CSV and PDF for reporting and sharing purposes.

Uploaded by

brsmce007
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 107

Module - 4

1. Django Admin Interfaces


2. Model Forms
3. Generic Views and Django State
Persistence
Django Admin
Interfaces
Deprecated
An Edit Form
displaying
errors
Form Processing
Search in Django
Implementing Search Functionality in a Book Application
Key Components in the View
The
Search
Template
The “Perfect Form”
Processing the form
• Once the user has filled the form to the point that it passes our validation rules, we
need to do something useful with the data.
• In this case, we want to construct and send an email containing the user’s feedback.
We’ll use Django’s email package to do this.
<tr>
<td colspan="2">
<ul class="errorlist"><li>Not enough words!
Please write at least 4 words.</li></ul>
</td>
</tr>
Generic
Views in
Django
What Are Generic views provide pre-built, reusable patterns for
common view functionality. Instead of writing views
Generic from scratch, you can use generic views to handle
repetitive tasks like displaying lists, showing details,
Views? or redirecting users.
• Django provides generic views to handle the following
common scenarios:
1. Simple Tasks
• Redirecting users to a specific page.

Common • Rendering a template with minimal logic.


2. List Views

Tasks • Displaying a list of objects from a model (e.g., a list


of blog posts, products, or users).

Handled • Example: Showing all events in an "Event List."


3. Detail Views

by
• Showing details of a single object.
• Example: Viewing a detailed page of a single event.

Generic
4. Date-Based Archives
• Displaying objects based on their publication dates
(e.g., archives for blog posts or news articles).
Views • Examples:
• Year archive: All articles published in a specific year.
• Month archive: Articles published in a specific
month.
• Day archive: Articles published on a specific day.
How Generic Views Work
Using Generic
Views in Django
• Django's generic views streamline the
development process by abstracting
common patterns. This section focuses
on how to configure and extend
generic views through URL patterns
and additional logic.
• Defining Generic Views in URLconf
• Generic views are configured in the
urlpatterns of your urls.py. Instead of writing
extensive view logic, you can pass extra
parameters in a dictionary to tailor the
behavior of the view.
• Example: Static "About" PageHere’s how
you can use the direct_to_template generic
view to serve a static page:
Using Generic
Views in
Django
• How It Works:
• The direct_to_template view
renders the specified template
(about.html in this case) without
requiring any additional logic.
• The extra parameter ('template':
'about.html') is passed as a
dictionary, which the generic
view uses for rendering.
• Why It Seems Magical:
• Although the code may feel
minimal, it's functionally
equivalent to defining a custom
view that loads and renders the
about.html template.
Extending
Generic
Views
• Generic views are just
functions. You can combine
them with your custom logic to
handle more complex
scenarios.
• Example: Dynamic "About"
Pages
• Suppose you want to serve
dynamic pages based on the
URL pattern (e.g., /about/team/
renders about/team.html).
• Step 1: Update urlpatternsAdd
a new pattern for dynamic
pages:
Generic Views for
Objects in Django
• Django’s generic views simplify
common patterns in database-
driven web development by
abstracting the creation of list and
detail pages. This allows
developers to focus more on the
functionality and design rather
than repetitive view logic.
• Using the Object List Generic
View
• Defining the Model
• Here’s the Publisher model used
in the example:
• Setting Up the URLconf
• To display a list of publishers, you can configure a URL pattern
that uses the list_detail.object_list generic view:
Extending Generic Views in
Django

• While generic views streamline


development and handle
common use cases, they are
not a one-size-fits-all solution.
• Many projects eventually
require customizations that go
beyond the capabilities of the
default generic views.
• Thankfully, Django provides
mechanisms to extend and
adapt generic views for more
complex scenarios.
Common Scenarios and Solutions
for Extending Generic Views
• Using extra_context
• The extra_context parameter allows you to add additional context to the
template. This is a dictionary of data to include alongside the default context.
• Example with Static QuerySet

• In this case, the template will have access to {{ publisher_list }}, containing all
publishers.
Use Callables
CSV and PDF
generation for
any models
Introduction
to Exporting
Data
• Why Export Data?
• For reporting, sharing, and
archiving purposes.
• Formats Covered:
• CSV: Tabular data format
for spreadsheets.
• PDF: Professional
document format for
printing and sharing.
Overview of the export_csv Function

RESPONSE SETUP: CREATE FILE NAME: DYNAMICALLY SET CSV WRITER: INITIALIZE A
AN HTTP RESPONSE WITH THE FILENAME USING THE CSV WRITER FOR WRITING
TEXT/CSV AS CONTENT TYPE. MODEL NAME. ROWS.
Dynamic Data Writing to CSV

Dynamic Data Retrieval: Header Row: Write column Data Rows: Loop through the
headers to the CSV. queryset and write each
Use if conditions to fetch records based on record to the file.
the model name.
Overview of the export_pdf Function

Response Setup: Set Canvas Initialization: Font Setup: Choose font


application/pdf as content Create a PDF canvas for type and size for
type. drawing content. consistency.
Adding Data
to the PDF
• Title: Add a title for the
report using drawString.
• Dynamic Content: Loop
through model data and
add it line by line.
• Line Spacing: Adjust the
y coordinate to manage
spacing.
Output

You might also like