Last active
February 7, 2022 00:32
-
-
Save jesusnoseq/4645052 to your computer and use it in GitHub Desktop.
Como hacer un menú de categorías en django.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class Categoria(models.Model): | |
slug = models.SlugField(blank=False,unique=True) | |
nombre = models.CharField(max_length=250,unique=True) | |
# no es el mejor modo de retornar la URL pero es facil de entender :D | |
def get_absolute_url(self): | |
return "/categoria/%s/" % self.slug | |
def __unicode__(self): | |
return u'%s' % self.nombre |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import template | |
from Principal.models import Categoria | |
register = template.Library() | |
@register.inclusion_tag('menu.html') | |
def getCategorias(): | |
cat = Categoria.objects.all() | |
return {'categorias':cat} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% load principal_extras %} <!-- Cargamos el tag --> | |
<!DOCTYPE html> | |
<html lang="es"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Prueba</title> | |
</head> | |
<body> | |
<div class="container"> | |
{% getCategorias %} <!-- llamamos al tag que hemos creado --> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment