django admin interface
django admin interface
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
• 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