Display objects in a respective html page

14 views
Skip to first unread message

Victor Santos

unread,
Dec 22, 2017, 10:18:24 AM12/22/17
to Django users
Hi everyone. I am trying to make a project which have an app named pet, in this app in the model I have "choice field"(I don't know if this is the best way to say that). So, I want to display the objects according to the choice value in a respective template. Therefore if I put meat with food "choice value", I would like display this objects in the respective template(like "food.html"). I am using get_absolute_url in my model because I want to share it with facebook(and I made some undesirable repetition in the url). Very thanks everybody. 

pet/models.py:
class Petmania(models.Model):
TIPOS = (
('COMER', 'Alimentacao'),
('EDUCA', 'Educacao'),
('ACESS', 'Acessorios'),
('HIGI', 'Higienizacao'),
('ANTI', 'Antiparasitas'),
)

tipos = models.CharField(max_length=6, choices=TIPOS, null=False)
titulo = models.CharField(max_length=100, null=False, blank=False)
apresentacao = models.TextField()
imagem = CloudinaryField('imagem', null=False, blank=False)
datapublicacao = models.DateTimeField(null=False, blank=False)

def publish(self):
self.publish_date = timezone.now()
self.save()

def __str__(self):
return self.titulo

def get_absolute_url(self):
return "petmania/%s" %(self.pk)

   pet/views.py:
def petmania(request):
objetos = Petmania.objects.filter(datapublicacao__lte=timezone.now()).order_by('datapublicacao')
return render(request, 'pet/index.html', {'objetos': objetos})

def post_detail(request, pk):
    objeto = get_object_or_404(Petmania, pk=pk)
    Petmania.objects.get(pk=pk)
    return render(request, 'pet/post_detail.html', {'objeto': objeto})

"""
def comida(request, pk):
objetos = Petmania.objects.filter(tipos='Alimentacao')
return render(request, 'pet/index.html', {'objetos': objetos})

def educacao(request, pk):
objetos = Petmania.objects.filter(tipos='Educacau')
return render(request, 'pet/index.html', {'objetos': objetos})

def acessorios(request, pk):
objetos = Petmania.objects.filter(tipos='Acessorios')
return render(request, 'pet/index.html', {'objetos': objetos})

def higiene(request, pk):
objetos = Petmania.objects.filter(tipos='Higienizacao')
return render(request, 'pet/index.html', {'objetos': objetos})

I try to use objects.filter as a way to select the objects to display, but I have no idea what is the correct way.
"""

and pet/urls.py
urlpatterns = [
url(r'^$', views.petmania, name='petmania'),
url(r'^petmania/(?P<pk>[0-9]+)/$', views.post_detail, name='detalhepets'),
]

to display in the html page, I am using the follow loop:
{% for objeto in objetos %}
{% if objeto.tipos == "COMER" %}
            <div class= 'ini'>
                <center><h2><p>Publicado em:{{ objeto.datapublicacao }}</p></h2><center>
                <center><h1><a href="{{ objeto.get_absolute_url }}">{{ objeto.titulo }}</a></h1></center>
                <center><img>{% cloudinary objeto.imagem %}</center>
                <center><p><h2>{{ objeto.apresentacao }}</h2></p></center>
            </div>
        {% endfor %}
{% endblock %}

My idea was do it in any page that I want display object, changing COMER by EDUCA, ACESS ... 

Very thanks for everyone. Have a nice day.
Reply all
Reply to author
Forward
0 new messages