Friday, December 22, 2017

Display objects in a respective html page

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.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/88c123e6-8133-4d4e-8f8e-22777a9cb8a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment