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.

nikjohn
  • 20,026
  • 14
  • 50
  • 86
  • The `arguments '()'` is the error that I get when I use `{% url 'details' article.id %}` The same exact error I get upon `{% url 'details' %}/article.id` This is why I think it's got something to do with the RegEx or the URLConf – nikjohn Oct 03 '15 at 15:50
  • `{% url 'details' article.id %}` matches the url pattern, as long as `article.id` is a valid id. As a test, try removing the url tag and including `{{ article }}` and `{{ article.id}}` inside the loop to see what they output. – Alasdair Oct 03 '15 at 16:09
  • `article` and `article.id` are coming up fine. I tried printing them on the screen outside the url tag. It's gotta be something with the URLConf I still think. Maybe an import or something is missing I don't know! – nikjohn Oct 03 '15 at 16:13
  • Should I be importing the views into my urls.py or something? – nikjohn Oct 03 '15 at 16:34
  • The recommended way is to import the views in your urls.py, but that's not the problem here. Using the string works until Django 1.10. – Alasdair Oct 03 '15 at 17:38
  • I have added a new section - please check and see if it makes any more sense – nikjohn Oct 03 '15 at 17:59
  • It doesn't make sense. It's not going to change from `'details' with arguments '()'` to `'details' with arguments '(1,)'` just because you changed the url patterns. Maybe you aren't restarting the server after changing code. You need to make sure that the arguments in the url tag match the arguments in the url pattern. Either include the article id in both, or leave it out from both. – Alasdair Oct 03 '15 at 18:29

2 Answers2

2

For the url pattern

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

The correct way to use the tag is

{% url 'details' article.id %}

This because the details url pattern has a group article_id, so you have to pass this to the tag.

If you have the above url pattern, and {{ article.id}} displays correctly in the template, then the above template tag should not give the error Reverse for 'details' with arguments '()'. That suggests you have not updated the code, or you have not restarted the server after changing code.

If you change the url pattern to

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

then you need to remove the article.id from the url tag.

{% url 'details' %}
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Thanks for commenting! I did not quite understand the first part of your answer though. {% url 'details' article.id %} is identical to what I have used. Am I missing something here? – nikjohn Oct 02 '15 at 22:10
  • If you look at the question, I have mentioned that: "I tried changing the way the param is passed to: {% url 'details' article.id %}" That didn't help :( Changed this in the question as well – nikjohn Oct 02 '15 at 22:21
  • I did look at the question - you said you had tried two ways, so I was telling you which was correct, and trying to explain why. If it still doesn't work, it doesn't mean the url tag is wrong, the problem could be somewhere else. – Alasdair Oct 02 '15 at 23:38
  • Right. I understand. I'm just trying to understand what's wrong with the urlconfig though – nikjohn Oct 03 '15 at 16:11
  • Should I be importing the views into my urls.py or something? – nikjohn Oct 03 '15 at 16:35
1

I guess your pattern is wrong.( not an expert of regex ). Try this

url(r'^details/((?P<article_id>[0-9]+)/$', 'news_readr.views.details', name='details'),
Sachin Gupta
  • 394
  • 3
  • 12
  • the parantheses are unbalanced no? I removed the extra open paranthesis and tried `url(r'^details/(?P[0-9]+)/$', 'news_readr.views.details', name='details'),` But that didn't do anything – nikjohn Oct 03 '15 at 15:25
  • Yeah the parantheses are unbalanced. If this is not case please check if article id correctly passed and id is set. You can also check by using article.pk instead of id. This is some minor bug and else everything is fine. – Sachin Gupta Oct 03 '15 at 16:09
  • article.pk gives the exact same error. I'm now convinced that the issue is either in the views.py or the urls.py .. The url tag seems to be fine. Do you see anything else that could possibly be wrong? – nikjohn Oct 03 '15 at 16:27
  • Should I be importing the views into my urls.py or something? – nikjohn Oct 03 '15 at 16:35
  • Yes you should be importing the views into urls.py. I normally do this from news_readr.view import * url(r'^details/(?P[0-9]+)/$', details, name='details'), – Sachin Gupta Oct 03 '15 at 17:13
  • I tried that. No use. I have added a new section - please check and see if it makes any more sense – nikjohn Oct 03 '15 at 17:59
  • Can you share your full urls.py ? I am using exactly this below line in my project as of now url(r'^comment/(?P[0-9]+)/$', views.CommentDetail, name='comment-detail'), – Sachin Gupta Oct 03 '15 at 18:12
  • Updated the question with the full urls.py – nikjohn Oct 03 '15 at 18:15