python - Django View with ForeignKey model -
i've got following in models.py:
class articlecategory(models.model): category_name = models.charfield("category", max_length=200, unique=true) slug = models.slugfield(null=true, blank=true, unique=true) def get_data(self): return { "id": self.pk, "category_name": self.category_name, "slug": self.slug, } class article(models.model): category = models.foreignkey(articlecategory) ... def get_data(self): return { "id": self.pk, ... }
views.py:
def articles(request, *args, **kwargs): context = { 'categories': articlecategory.objects.all(), 'articles': article.objects.filter(category__id=2) } return render(request, 'wellness.html', context)
and works fine. however, want category__id dynamic, i'm changing function def articles(request, id, *args, **kwargs)
, 'articles': article.objects.filter(category__id=id)
seems working previous projects me, throws error every time try set such behavior (tried id, slug, category_name):
exception type: typeerror exception value: articles() takes @ least 2 arguments (1 given) exception location: .../env/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py in _wrapped_view, line 23
maybe urls.py?
from django.contrib.auth.decorators import login_required ... urlpatterns = [ ... url(r'^wellness$', login_required(articles), name='wellness'), ... ]
yes indeed it's in urls.py.
you defined view expects 2 parameters didn't configure url
function capture part of url , pass on additional parameter next request
.
you want capture slug or id respectively:
slug
from django.conf.urls import url urlpatterns = [ url(r'^wellness/(?p<slug>[-\w]+)/?$', login_required(articles), name='wellness'), url(r'^reports/(?p<id>[0-9]+)/$', credit_views.report),# kwargs = {"id": 9} ]
alternatively id:
urlpatterns = [ url(r'^wellness/(?p<id>[0-9]+)/$', login_required(articles), name='wellness'), ]
in documentation can find more information.
Comments
Post a Comment