0

I'm new to Django, and I'm developing a website in which I need to automate the creation of new pages through Django admin platform. So the idea is to create a new tab on the main menu of the site if a display parameters is set to True.

However I want to give the user the option to create a dropdown menu like this one, Dropdown menu.

So my idea is to have a checkbox Dropdown Menu, that whenever the user sets it True, a list of all the available pages to "nest" under the dropdown menu will be automatically enabled below it.
I'm attaching the admin page as it is today,Admin fields..


models.py of app creat_pages

class CreatePage(models.Model):
    name = models.CharField("Nome", max_length=100)
    slug = models.SlugField("Atalho")

    tittle = models.CharField("Título", max_length=100)
    description = models.TextField("Descrição", blank=True)
    body = models.TextField("Corpo", blank=True)
    display = models.BooleanField("Mostrar no Site", default=False)
    dropdown_menu = models.BooleanField("Dropdown Menu", default=False)
    dropdown_options = models.ForeignKey("self", null=True, related_name='dropdown', 
                                  on_delete=models.CASCADE)
    created_at = models.DateTimeField("Criado em", auto_now_add=True)
    updated_at = models.DateTimeField("Última alteração", auto_now=True)

    objects = models.Manager()

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        from django.urls import reverse

        return reverse("create_pages:detail", kwargs={"slug": self.slug})

    class Meta:
        verbose_name = "Criar Página"
        verbose_name_plural = "Criar Paginas"
        ordering = ["-name"]

Admin.py:

class CreatePageAdmin(admin.ModelAdmin):
    list_display = ["name", "slug", "tittle", "display", "created_at"]
    search_fields = ["name", "slug", "tittle"]
    prepopulated_fields = {"slug": ("name",)}

admin.site.register(CreatePage, CreatePageAdmin)
Itamar Mushkin
  • 2,803
  • 2
  • 16
  • 32
Leonardo Guerreiro
  • 901
  • 1
  • 9
  • 18

1 Answers1

0

Solved it. I just needed to know how to use Foreignkey. After I learned that it was pretty straight forward:

I created a context_processor.py that filters all pages that have display = True, and makes them available to all my templates' context. In this way I can render the pages pretty easily.

from .models import CreatePage  
def new_page(request):     
    display = CreatePage.objects.filter(dropdown__isnull=True, display=True)      
    return {"display": display}
Sam Carlson
  • 1,891
  • 1
  • 17
  • 44
Leonardo Guerreiro
  • 901
  • 1
  • 9
  • 18