Django模板中如何自定义模型对象字段的显示值?
Got it, let's fix this for you! When you need to show a human-friendly label instead of the raw stored value (like community001) in your template, there are a few reliable approaches to try—here are the most straightforward ones:
1. Add a Custom Method to Your Model (Recommended)
This is the cleanest approach because it keeps display logic tied directly to your model, making it reusable across all templates and views.
In your Campaign model, add a method that maps the raw value to the desired display text:
from django.db import models class Campaign(models.Model): # Your existing fields here project_focus = models.CharField(max_length=50) # Match your actual field type def get_project_focus_display(self): # Create a mapping of raw values to friendly labels focus_mapping = { 'community001': 'Community Project', # Add other mappings if you have more values, e.g.: # 'education001': 'Education Initiative', # 'environment001': 'Environmental Campaign' } # Return the friendly label, or the raw value if no match is found return focus_mapping.get(self.project_focus, self.project_focus)
Then in your HTML template, instead of {{ campaign.project_focus }}, use:
{{ campaign.get_project_focus_display }}
2. Create a Custom Template Filter
If you prefer to keep display logic within the template layer, a custom filter works great.
First, create a templatetags directory in your app (make sure it has an empty __init__.py file), then add a file like custom_filters.py:
# your_app/templatetags/custom_filters.py from django import template register = template.Library() @register.filter(name='focus_label') def focus_label(value): focus_mapping = { 'community001': 'Community Project', # Add other mappings here } return focus_mapping.get(value, value)
In your template, load the filter first at the top, then use it on your field:
{% load custom_filters %} {{ campaign.project_focus|focus_label }}
3. Modify the QuerySet in Your View
If you want to handle the mapping in your view before passing data to the template, you can add a custom attribute to each campaign object:
# In your view function from .models import Campaign def your_view(request): localcampaigns = Campaign.objects.all() # Loop through each campaign and add a friendly label attribute for campaign in localcampaigns: focus_mapping = { 'community001': 'Community Project' } campaign.friendly_focus = focus_mapping.get(campaign.project_focus, campaign.project_focus) # Pass the modified queryset to the template return render(request, 'your_template.html', {'localcampaigns': localcampaigns})
Then in your template, use the new attribute:
{{ campaign.friendly_focus }}
Why Your Previous Attempt Might Have Failed
- Double-check that your mapping dictionary has the exact raw value (case-sensitive!) from the database—
community001must match exactly what's stored. - If you tried a model method, make sure you're calling it correctly in the template (no parentheses needed in Django templates).
- If using a template filter, ensure you loaded the filter at the top of your template and spelled the filter name correctly.
内容的提问来源于stack exchange,提问作者Derrick Sherrill

