======= H-Cards ======= H-card is a microformat for representing people and organizations on the web. Django-indieweb provides models, template tags, and utilities for managing and displaying h-card data. Features ======== * **Profile model**: Store h-card data with flexible JSON storage * **Template tags**: Render h-cards with proper microformat markup * **Admin interface**: Edit h-card data with a user-friendly JSON editor * **Parsing utilities**: Extract h-card data from HTML * **Validation**: Ensure h-card data follows microformats2 specifications Quick Start =========== 1. Create a profile for your users: .. code-block:: python from django.contrib.auth import get_user_model from indieweb.models import Profile User = get_user_model() user = User.objects.get(username='alice') profile = Profile.objects.create( user=user, name="Alice Johnson", photo_url="https://example.com/alice.jpg", url="https://example.com", h_card={ "name": ["Alice Johnson"], "photo": ["https://example.com/alice.jpg"], "url": ["https://example.com", "https://social.example/@alice"], "email": ["alice@example.com"], "note": ["Web developer and IndieWeb enthusiast"], "adr": [{ "locality": "Portland", "region": "OR", "country-name": "USA" }] } ) 2. Display the h-card in your templates: .. code-block:: django {% load indieweb_tags %}
This will render a properly formatted h-card with microformats2 markup. Profile Model ============= The ``Profile`` model stores h-card data for users: .. code-block:: python class Profile(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL, ...) h_card = models.JSONField(default=dict, blank=True) # Common fields for quick access name = models.CharField(max_length=200, blank=True) photo_url = models.URLField(blank=True) url = models.URLField(blank=True) The ``h_card`` field stores all h-card properties as JSON, following the microformats2 structure where all values are arrays. .. note:: The common fields (``name``, ``photo_url``, ``url``) are automatically synchronized with the h_card data when saving. If you update the h_card JSON, these fields will be updated automatically. Property Name Normalization --------------------------- When h-card data is parsed or displayed in templates, property names containing hyphens are automatically converted to underscores for Django template compatibility: * ``country-name`` → ``country_name`` * ``street-address`` → ``street_address`` * ``postal-code`` → ``postal_code`` This happens automatically in: * The ``parse_h_card()`` function * The ``{% h_card %}`` template tag * The ``normalize_h_card()`` function When storing h-card data in the database, you can use either format - both will work correctly. H-Card Properties ----------------- Common h-card properties you can store: * ``name``: Full name (array of strings) * ``photo``: Profile photo URLs (array of strings or objects with ``value`` and ``alt`` properties) .. code-block:: json { "photo": [ "https://example.com/photo1.jpg", {"value": "https://example.com/photo2.jpg", "alt": "Profile photo"} ] } * ``url``: Personal URLs (array of strings) * ``email``: Email addresses (array of strings) * ``tel``: Phone numbers (array of strings) * ``note``: Biography or description (array of strings) * ``nickname``: Nicknames or handles (array of strings) * ``adr``: Addresses (array of address objects) * ``org``: Organizations (array of organization objects) Address Format ~~~~~~~~~~~~~~ .. code-block:: json { "adr": [{ "street-address": "123 Main St", "locality": "Portland", "region": "OR", "postal-code": "97201", "country-name": "USA" }] } Organization Format ~~~~~~~~~~~~~~~~~~~ .. code-block:: json { "org": [{ "name": "IndieWeb Corp", "url": "https://indieweb.example" }] } Template Tag ============ The ``h_card`` template tag renders h-card microformats: .. code-block:: django {% load indieweb_tags %} {# Render h-card for a user #} {% h_card user %} {# Render h-card for a profile #} {% h_card profile %} The tag accepts either a User object (which will look up the associated profile) or a Profile object directly. Rendered HTML Example --------------------- .. code-block:: html
Web developer and IndieWeb enthusiast
Software engineer
{{ note }}
{% endfor %}