I am creating a simple website with Django, where I'm using template inheritance. The "master page" model contains information that is shown on all pages, like a header or footer. I can't hardcode it, since I need it to be editable from the admin pages.
At the moment (some of) my models look somewhat like this:
class MasterContent(models.Model):
title_header = models.CharField(max_length=200)
name_header = models.CharField(max_length=200)
class ProfileContent(models.Model):
description = models.TextField()
profile_picture = models.ImageField(upload_to='profile')
Then I render to a view like so:
def profile(request):
return content_shortcut(ProfileContent, 'pages/profile.html')
def content_shortcut(content_class, template):
return render_to_response(template, {'master_content':MasterContent.objects.get(id=1), 'content':content_class.objects.get(id=1)})
Now, I need to render a view from a different app, but I still want the master page content to be passed along. It will become way too messy for my liking if I have to manually include the master_page
content every time I render a view (even with the shortcut it seems wrong). I tried making MasterContent abstract, but this forces me to re-enter the data in every other model that inherits from it, which is a much worse scenario.
In short: how can I easily have data available in all the views, across apps, which can be edited from a single place in the admin pages, while keeping things DRY!
Note: I also think there must be a more smooth way (something like flatpages) to administrate the content of the profile page than having a model of which I will only ever use one instance/row, but I chose to ignore this for now...
Sounds like an inclusion tag
or even a simple tag
should do the job just fine.
You could query the content in the tag and embed the tag in your base template.
And if you need this information on every page context processors
might be another option, though template tags are probably preferable for your use case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With