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!
fjournalvariable to the context dict. This dict{'articles': articles}only has thearticlesvariable in it. Add thefjournalargument to it as well.fjournaladding to that context dict? And no, there shouldn't be just one variable there. You can add as many as you want.