Reputation: 144
I tried get the id from my database and make something like /article/1/
. 1
is the id of my article but it didn't work.
views.py
def article(request, article_id):
return render_to_response('article.html', {{'article': Articles.objects.get(id=article_id)}})
my urls.py
from django.urls import path, re_path
from . import views
urlpatterns = [
path('showall/', views.articles, name='articles'),
path('<int:article_id>/', views.articles, name='article'),
]
I get the error:
TypeError at /article/1/ articles() got an unexpected keyword argument 'article_id'
I also include image of my datebase
Upvotes: 0
Views: 1460
Reputation: 253
You are using views.articles in article/id/ url which most likely you have used for article/showAll/ url. Use this instead:
path('<int:article_id>/', views.article, name='article')
Upvotes: 0
Reputation: 308769
It looks as if you are using views.articles
for the article detail view:
path('<int:article_id>/', views.articles, name='article'),
You should use views.article
instead:
path('<int:article_id>/', views.article, name='article'),
Note that there are a few improvements/fixes you can make to your article
view:
get_object_or_404
so that you don't get a server error when the article does not exist in the databaserender
instead of the obsolete render_to_response
{...}
. You currently have double curly brackets {{...}}
Putting that together you get:
from django.shortcuts import get_object_or_404, render
def article(request, article_id):
article = get_object_or_404(Article, id=article_id)
return render(request, 'article.html', {'article': article})
Upvotes: 3
Reputation: 2780
Your views.py
defines an article
function while your urls.py
makes use of an articles
(note the plural) function. Is this a typo, or is there any articles
function in views.py
that does not support an optional article_id
parameter?
Upvotes: 0