0

I am trying to pass configure a URL like so:

/details/12345

Template HTML:

    <div class="row">
    {% if article_list %}
    {% for article in article_list %}
    <div>
      <h2>{{ article.title }}</h2>
      <p>{{ article.body }}</p>
      <p><a class="btn btn-default" href="{% url 'details' article.id %}" role="button">View details &raquo;</a></p>
    </div><!--/.col-xs-6.col-lg-4-->
    {% endfor %}
    {% endif %}
  </div><!--/row-->

urls.py (full):

    from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', 'news_readr.views.home', name='home'),
    url(r'^details/(?P<article_id>\d+)/$', 'news_readr.views.details', name='details'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

views.py:

from django.shortcuts import render
from .models import Article

# Create your views here.
def home(request):
    title = "Home"
    article_list = Article.objects.all()
    for article in article_list:
        print(article.id)
    context = {
               "title": title,
               "article_list": article_list,
               }
    return render(request, "home.html", context)

def details(request, article_id = "1"):
    article = Article.objects.get(id=article_id)
    return render(request, "details.html", {'article': article})

I am getting an error that says:

 NoReverseMatch at /

Reverse for 'details' with arguments '()' and keyword arguments '{}'
not found. 1 pattern(s) tried: ['details/(?P<article_id>\\d+)/$']

I'm one week old at Django, and I think there's something wrong with my URL Named Group config. Please help! TIA!

Update: If I remove the URL config and change it back to:

url(r'^details/$', 'news_readr.views.details', name='details'),

The error changes to:

Reverse for 'details' with arguments '(1,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['details/$']

So it seems to be picking up the argument that is passed 1 in this case. So this seems to be an issue with the regular expression. I tried the expression out at Pythex, but even there, the expression doesn't seem to be matching anything.

4

2 回答 2

2

对于 url 模式

url(r'^details/(?P<article_id>\d+)/$', 'news_readr.views.details', name='details'),

使用标签的正确方法是

{% url 'details' article.id %}

这是因为detailsurl 模式有一个 group article_id,所以你必须将它传递给标签。

如果你有上面的 url 模式,并且{{ article.id}}在模板中正确显示,那么上面的模板标签不应该给出错误Reverse for 'details' with arguments '()'。这表明您尚未更新代码,或者您在更改代码后尚未重新启动服务器。

如果您将 url 模式更改为

url(r'^details/$', 'news_readr.views.details', name='details')

那么您需要article.id从 url 标记中删除 。

{% url 'details' %}
于 2015-10-02T22:01:24.333 回答
1

我猜你的模式是错误的。(不是正则表达式的专家)。尝试这个

url(r'^details/((?P<article_id>[0-9]+)/$', 'news_readr.views.details', name='details'),
于 2015-10-03T07:45:55.960 回答