当我点击Django应用程序中的标签时,如何链接到特定帖子

时间:2016-01-30 02:57:43

标签: python django

在我的详细信息页面上,我在列中有一个标记列表。我想要的结果是当我点击该列中的链接时,它会将我带到与其对应的帖子。

到目前为止我尝试过的是什么

post_detail.html

{% block content %}

<div class="row" style="margin-top: 70px">

    <div class="col-sm-8">
        {% if instance.image %}
            <img src='{{ instance.image.url }}' class="img-responsive" />
        {% endif %}


    <p>Share on:
    <a href="https://www.facebook.com/sharer/sharer.php?u={{ request.build_absolute_uri }}">
    Facebook
    </a>

    <a href="https://twitter.com/home?status={{ instance.content | truncatechars:80 | urlify }}%20{{ request.build_absolute_uri }}">
     Twitter
    </a>


    <a href='https://plus.google.com/share?url={{ request.build_absolute_uri }}'></a>


    <a href="https://www.linkedin.com/shareArticle?mini=true&url={{ request.build_absolute_uri }}&title={{
    instance.title }}&summary={{ share_string }}&source={{ request.build_absolute_uri }}">
     Linkedin
    </a>
    </p>

        <h1>{{ title }}<small>{% if instance.draft %}<span style="color:red"> Draft</span>{% endif %} {{instance.publish}}</small></h1>


        {% if instance.user.get_full_name %}
        <p>By {{ instance.user.get_full_name }}</p>
        {% else %}
            <p>Author {{ instance.user }}</p>
        {% endif  %}

        <p><a href='{% url "posts:list" %}'>Back</a></p>
        <p><a href='{% url "posts:delete" instance.id %}'>delete</a></p>
        <p>{{instance.content | linebreaks }}</p>
        <hr>


    <noscript>Please enable JavaScript to view the
        <a href="https://disqus.com/?ref_noscript" rel="nofollow">comments powered by Disqus.</a>
    </noscript>
    <script id="dsq-count-scr" src="//hispanic-heights.disqus.com/count.js" async></script>

    </div>

    <div class="panel panel-default pull-right" style="height: 1000px">
        <div class="panel-heading">
        <h3 class="panel-title">Similar Articles</h3>
      </div>
        <div class="panel-body">
            {% for tag in instance.tags.all %}
            <h4> <a href="{% url 'posts:detail' slug=tag.slug %}"> {{ tag.title }}</a>  </h4><hr>
            {% endfor %}
        </div>
    </div>


</div>

{% endblock content %}

这是我得到的错误

No Post matches the given query.

我也试过

tag.slug
tag.get_absolute_url

以及其他一些变化。但没有任何作用,继承人我的 urls.py

urlpatterns = [
 url(r'^$',   post_list, name='list'),
 url(r'^tag/(?P<slug>[\w-]+)/$', tag_list, name="tag_list"),
 url(r'^create/$', post_create, name='create'),
 url(r'^sewp$', sewp, name='sewp'),
 url(r'^(?P<slug>[\w-]+)/$', post_detail, name='detail'),
 url(r'^(?P<slug>[\w-]+)/edit/$', post_update, name='update'),
 url(r'^(?P<id>\d+)/delete/$', post_delete, name='delete'),
]

1 个答案:

答案 0 :(得分:0)

我不认为这是可能的。你混合了两件事,命名相同,但代表不同的东西:

  • Post.slug这是给定帖子的slu ..
  • Tag.slug这是给定标签的slu ..

你无法从标签的slug进入帖子页面。