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
