Django URL in template with slug -
i have template (home.html) want list items in model, , when click on them, take user details page of item.
home.html:
{% item in menuitems %} <div class="col-md-4"> <h2><a href="{% url 'show_menuitem' slug=instance.slug %}">{{ item }}</a></h2> <p> </p> </div> {% endfor %}
urls.py:
urlpatterns = [url(r'^menuitem/(?p<menuitem_slug>[-\w]+)/$', menu_views.show_menuitem, name = 'show_menuitem'), ]
views.py:
def home(request): page_title = 'samplepagetitle' template_name="home.html" menuitems = menuitem.objects.all() context = { 'page_title':page_title, 'menuitems':menuitems, } return render(request, template_name, context)
models.py:
class menuitem(models.model): name = models.charfield(max_length=250, blank=false, unique=true) slug = models.slugfield(max_length=250, unique=true, def __str__(self): return self.name @models.permalink def get_absolute_url(self): return ('show_menuitem', (), { 'menuitem_slug': self.slug })
i keep getting noreversematch error, says:
reverse 'show_menuitem' keyword arguments '{'slug': ''}' not found. 1 pattern(s) tried: ['menuitem/(?p[-\w]+)/$']
why giving me error? loop set wrong in template file?
in loop call each object item
. in url tag refer instance
, not exist. should use item
there too.
{% url 'show_menuitem' menuitem_slug=item.slug %}
Comments
Post a Comment