how to display models in templates

36 views
Skip to first unread message

Timothy Steele

unread,
Sep 13, 2016, 3:33:53 AM9/13/16
to Django users
please i try my best but i can display my models in a templates i have just created.

Please have a look at the codes in the models.py file

from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class Link(models.Model):
    url=models.URLField(unique=True)

    def __str__(self):
        return self.url



class Bookmark(models.Model):
    title=models.CharField(max_length=200)
    user=models.ForeignKey(User)
    link=models.ForeignKey(Link)
    def __str__(self):
        return self.title

Please have a look at the codes in the  templates file call user_page.html

head>
    <meta charset="UTF-8">
    <title>Django Bookmarks - User:{{ username }}</title>
</head>
<body>
    <h1>Bookmarks for {{ username }}</h1>
    {% if bookmarks %}
       <ul>
          {% for bookmark in bookmaks %}
           <li>{{ bookmarks.title}}</li>
          {% endfor %}
       </ul>
    {% else %}
            <p>No bookmark found.</p>
    {% endif %}


</body>

This the codes from the views.py file (the object of the user_page)


from django.shortcuts import render
from django.http import HttpResponse,Http404
from django.template import Context
from django.template.loader import get_template
from django.contrib.auth.models import User
from . models import Bookmark,Link

def user_page(request, username):

   try:
        user=User.objects.get(username=username)

    except:
        raise Http404('Requested user not found.')
    bookmarks=user.bookmark_set.all()
    template=get_template('user_page.html')
    variables=Context({
        'username':username,
        'bookmarks': bookmarks
    })
    output=template.render(variables)
    return HttpResponse(output)

i try it out but it only give me this


ludovic coues

unread,
Sep 13, 2016, 3:45:33 AM9/13/16
to django...@googlegroups.com
Can you try this in your template ?

{{ bookmarks|pprint }}
{% for bookmark in bookmaks %}
   <br>{{ bookmark|pprint }}
{% endfor %}

Also, in your for loop, you use bookmarks.title, notice the s.

--
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...@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/13bc97fb-359d-4be7-b1b9-9cdb84a4a3a9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Timothy Steele

unread,
Sep 13, 2016, 4:00:12 AM9/13/16
to Django users

please i like your help but it now return in the database, but in can see the name of the bookmark but how can i do away with the QuerySet please
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

ludovic coues

unread,
Sep 13, 2016, 4:15:53 AM9/13/16
to django...@googlegroups.com
Sorry, I should have double checked instead of copy/pasting.
{% for bookmark in bookmaks %} < you forgot a r in bookmarks.

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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

Timothy Steele

unread,
Sep 13, 2016, 4:30:44 AM9/13/16
to Django users
thanks i have seen it, but i need to only display  Packt Publishing but it give this <Bookmark: Packt Publishing> in addition 

ludovic coues

unread,
Sep 13, 2016, 4:49:21 AM9/13/16
to django...@googlegroups.com
Yeah, it's because of |pprint. It is a debug tools. It can help with a few issue like model not having a __str__ method (not your case). It also show how many item are in the queryset (one in your case).

Now that you found the error, you can remove it

--
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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

Timothy Steele

unread,
Sep 13, 2016, 5:01:00 AM9/13/16
to Django users
thank you very mach it has work. i am most grateful, any way this did not let me sleep the hole night 


On Tuesday, September 13, 2016 at 8:33:53 AM UTC+1, Timothy Steele wrote:
Reply all
Reply to author
Forward
0 new messages