Home > Blockchain >  How do I create a dynamic "About Me" page in Django?
How do I create a dynamic "About Me" page in Django?

Time:06-05

Here, by dynamic I mean, I wouldn't want to update my template in order for me to update changes. I'd want them to edit in the admin page on my production site. At first, I thought I'd create a model for "About Me" itself, but then I'd need to create a model for just one instance.

I need help with this, better ways to edit my pages dynamically on the admin site.

CodePudding user response:

Perhaps you could create a model for About Me as you mentioned. Since you've highlighted that you would want to work with the django admin site, then what you could do is to set the permission for only one object to be created for that model which can be updated whenever.

For example:

models.py file.

class AboutMe(models.Model):
     # With the desired fields of your choice

Now you can set the permission within the admin.py file to only allow one instance from the model to be created.

from django.contrib import admin
from .model import AboutMe

MAX_OBJECTS = 1

# Using decorator here
@admin.register(AboutMe)
class AboutMeAdmin(admin.ModelAdmin):
     fields = ['..', '..', '..'] # fields you want to display on the forms
     list_display = ['..', '..', '..'] # fields you want to display on the page for list on objects

     # Allowing the user to only add one object for this model...
     def has_add_permission(self, request):
          if self.model.objects.count() >= MAX_OBJECTS:
               return False
          return super().has_add_permission(request)

That should be a nice fit for your situation. Also, you can read the docs to learn more about customizing django admin site.

  • Related