1

I am somewhat new to Django and I figured I would start of by building a standard library app.

What I had initialy created was a url pattern using slugs that shows you all the articles in a journal via http://127.0.0.1:8000/Albums/[album name]/.

This worked. What I then tried to do was make the article names clickable so that they would refer you to a page containing details about the song. Particularly I wanted these details to be accessible via a link of the form http://127.0.0.1:8000/Albums/[article name]/[article_id]/. Where article_id at the moment is the primary key in a database table containing all articles.

In order to do so I defined two url patterns:

urlpatterns = [
    url(r'^(?P<slug_journal>[\w-]+)/$', views.Journal_Article_Page, name='Journal_Page'),
    url(r'^(?P<slug_journal>[\w-]+)/(?P<id>\d+)/$', views.Journal_Article_Page, name='Journal_Article_Page'),
]

and a view:

def Journal_Article_Page(request, slug_journal=None, id=None):
    if slug_journal:
        if id:
            farticle = get_object_or_404(Article_Draft_Table_1, DRAFT_ID = id)
            return render(request, 'journal_article_details.html', { 'farticle': farticle, 'fjournal': slug_journal })
        else:
            fjournal = get_object_or_404(Journal_Table, Journal_slug = slug_journal).JOURNAL_ID
            articles = Article_Draft_Table_1.objects.filter(JOURNAL_ID = fjournal)
            return render(request, 'journal_articles.html', { 'articles': articles })

And finally a template:

{% extends 'base.html' %}

{% block content %}

    <p>List of articles.</p>

    <br>

    {% for article in articles %}
        <div class="col-sm-10">
        <a href='{% url "Journal_Article_Page" slug_journal=fjournal id=article.DRAFT_ID %}'>{{ article.Draft_title }}</a>
        </div>
    {% endfor %}

{% endblock %}

I then end up with thw following error:

Reverse for 'Journal_Article_Page' with arguments '()' and keyword arguments '{'id': 3, 'slug_journal': ''}' not found. 1 pattern(s) tried: ['Journals/(?P<slug_journal>[\\w-]+)/(?P<id>\\d+)/$']

Which to my appears to imply that the slug_journal string is not passed on correctly. In fact if I swap out fjournal in the template with the actual string of a particular journal name everything does work. I have now tried a number of different ways to solve this problem but can't seem to find a solution. I am in fact not even sure if the value is not passed on or whether I am not refering to it correctly in the template. Any help would be appreciated!

4
  • 1
    You're not passing fjournal variable to the context dict. This dict {'articles': articles} only has the articles variable in it. Add the fjournal argument to it as well. Commented Nov 27, 2016 at 16:21
  • That part corresponds to the list, I think there should be only one argument there no? Or does it somehow mess with the other option of the if statement? Commented Nov 27, 2016 at 18:19
  • Did you try adding fjournal adding to that context dict? And no, there shouldn't be just one variable there. You can add as many as you want. Commented Nov 27, 2016 at 19:17
  • My bad, you are correct. I had just completely misunderstood what you were pointing to. This did indeed solve my problem. Thanks a million! Commented Nov 28, 2016 at 14:56

2 Answers 2

3

You have not included a fjournal value in the context, so the template engine will just use an empty string. The pattern expects at least one character in the slug [\w-]+ so the empty slug string will not match any url pattern.

You must include fjournal into the context dictionary. Something like this should probably work.

return render(request, 'journal_articles.html', 
              {'articles': articles, 'fjournal': slug_journal})

In addition, your code will be easier to read and maintain if you split the article list and the article itself into two different view functions instead of using Journal_Article_Page for both.

It's also convention to use only lower case letters in function names. So you could call the functions article_page and journal_article_list or something like that.

Sign up to request clarification or add additional context in comments.

2 Comments

This solved my problem! thanks. Could you perhaps say something more about why turning this into two seperate views would be better according to you?
It's a subjective opinion. Code that is clear and explicit is easier to maintain and debug. One url route -> one view function. Simple is better than complicated. Flat is better than nested. Readability counts. python.org/dev/peps/pep-0020
1

Django tries each patterns in urlpatterns and returns first which corresponds to request. I believe just swap urls in urlpatterns will help:

urlpatterns = [    
url(r'^(?P<slug_journal>[\w-]+)/(?P<id>\d+)/$', views.Journal_Article_Page, name='Journal_Article_Page'),
url(r'^(?P<slug_journal>[\w-]+)/$', views.Journal_Article_Page, name='Journal_Page'),
]

2 Comments

Thanks for the response. I tried swapping the url patterns, sadly it gave me the exact same error.
@DisneylandSC seems like you also should add fjournal value into context as Haken Lid said in his answer and swap urls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.