我想知道如何通过此模板URL在论坛视图中传递另一个参数?

时间:2014-07-04 17:48:13

标签: django django-models django-templates django-admin django-views

这是我的模板文件

<div id="list">
<table border="3px solid black" cellpadding="4" width ="100%">
    <tr>
        <td><div></div> </td>
        <td>Posts</td>
        <td>Last Post</td>
        <td><div></div> </td>
    </tr>

{% for forum in forums %}
<tr>
    <td {% if forloop.last %}class = "last"{% endif %}>
            <div class="title"> {{ forum.title }}
            </div>
    </td>
    <td {% if forloop.last %} class="last" {% endif %}>{{ forum.num_posts }} </td>
    <td {% if forloop.last %} class="last" {% endif %}>{{ forum.last_post.short|linebreaksbr }} </td>
    <td {% if forloop.last %} class="last" {% endif %}}> <a href="{% url 'frm.views.forum' %}"> VIEW </a> </td>
</tr>

{% endfor %}

这是views.py

中的一个函数
def forum(request,pk):
threads = Thread.objects.filter(forum=pk).order_by("-created")
threads = mk_paginator(request,threads,20)
return render_to_response("forum.html",add_csrf(request,threads=threads))

我想通过模板文件传递这个额外的参数。 我怎么能这样做?

来自urls.py文件

from django.conf.urls import patterns, include, url
from frm.views import main,forum

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$',main),
    url(r'^forum/$',forum),
    # Examples:
    # url(r'^$', 'forum.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
)

1 个答案:

答案 0 :(得分:0)

改变这个:

url(r'^forum/$', forum)

这样,url模式必须接受一个参数id:forum

url(r'^forum/(?P<pk>\d+)/$', forum)

并在url标记传递论坛ID:

{% url 'frm.views.forum' forum.pk %}