0

I have made a Django application to show a list of clustered news articles. I want in the first page to show just a three of articles, and if the user wants to show them all, he can click the "See the real coverage" link and see the overall list of the articles (as GoogleNews functions). In the main page, I have the list of articles in a variable called lista. And I want to publish only the first three elements in the main page, and I want to transfer the list of articles in the the new page which, let's, I call theme. I add the theme function in the views.py file as:

def theme(request,argument):
    lista=argument
    return render(request,'theme.html', locals())

And the main.html code where i call the theme function is as follows:

<a href="{% url "mysite.views.theme" argument=lista %} target="_blank">

or I try:

<a href="{% url "mysite.views.theme" lista %} target="_blank">

The urls.py code is:

urlpatterns = patterns('',
    ('^main/$', main),
    ('^all/$', tegjitha),
    ('^(sport)/$', gen),
    ('^(teknologji)/$', gen),
    ('^(showbiz)/$', gen),
    ('^(bota)/$', gen),
    ('^(ekonomi)/$', gen),
    ('^(kulture)/$', gen),
    ('^(theme)/$', theme),

It gives me: NoReverseMatch at /main/ error.

4

2 回答 2

2

你需要意识到你试图采取的方法是行不通的。模板标签所做的url只是生成一个简单的字符串——特定资源的 URL。所以标签的结果可能是一个字符串,如“/post”、“/post/24”等。就是这样。除了生成 URL 字符串之外,该函数不做任何事情,并且在简单的 Web URL 中没有复杂的 Python 对象列表。

您还需要意识到,在最基本的层面上,网络是无状态的。您不能只在请求之间传递 python 对象,因为每个请求都是完全独立的。您可以使用会话模拟在请求之间保持状态,但我认为在这种特殊情况下这不是正确的工具。

相反,您应该为每个列表设置一个唯一标识符并在 URL 中传递标识符(与在 URL 中传递整个列表相反)。然后,您将在视图中使用标识符,再次检索列表并显示结果。

于 2013-10-26T11:06:02.130 回答
0

您需要将 urls.py 定向为绝对字符串,就像模板中的 url 标签一样。所以 main 看起来像这样:

('^main/$', 'mysite.views.main'),

您还可以在顶部定义模式的路径:

urlpatterns = patterns('mysite.views',
    ('^main/$', 'main'),
    … etc

但请注意,您仍然需要将其作为字符串引用

于 2013-10-26T10:15:02.410 回答