0

在我的用户页面中,当我单击已添加书签的链接时,它不会重定向到其各自的 URL。请指导我。谢谢你。

我的意见.py:

def user_page(request, username):
    try:
        user = User.objects.get(username=username)
    except:
        raise Http404('Reqested user not found.')

    bookmarks = user.bookmark_set.all()

    variables = RequestContext(request, {
        'username':username,
        'bookmarks':bookmarks
        })
    return render_to_response('user_page.html',variables)

我的 user_page.html:

{% extends "base.html" %}
{% block title %} {{username}} {% endblock %}
{% block head %} Bookmarks for {{username}} {% endblock %}
{% block content %}
    {% if bookmarks %}
        <ul>
            {% for bookmark in bookmarks %}
                <li><a href="{{bookmark.url.link}}">{{bookmark.title}}</a></li>
            {% endfor %}
        </ul>
    {% else %}
        <p>No bookmarks found.</p>
    {% endif %}
{% endblock %}

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

    def __unicode__(self): 
        return '%s, %s' % (self.user.username, self.link.url)
4

1 回答 1

0

它应该是

{{bookmark.link.url}}

反对

{{bookmark.url.link}}

由于 FK 字段bookmark为 islink而不是url

于 2013-07-12T23:33:03.560 回答