2

Started learning Django a few days ago, came across this book "Tango with django" and started following it. But I am stuck here..might be a silly error with the pattern matching.. When I click a category, the relevant Category pages should be shown snapshot But the following error is shown : Error image

/urls.py

from django.conf.urls import url
from django.contrib import admin


from django.conf.urls import url,include
from rango import views
from django.conf.urls.static import static



urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^rango/', include('rango.urls')),

]

rango/urls.py

from django.conf.urls import url
from rango import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^about/', views.about, name='about'),
url(r'^add_category/$', views.add_category, name='add_category'),
url(r'^category/(?P<category_name_slug>[\w\-]+)/', views.show_category, name='show_category'),
url(r'^category/(?P<category_name_slug>[\w\-]+)/add_page/', views.add_page, name='add_page'),
]

views.py

   from django.http import HttpResponse
    from django.template import RequestContext
    from django.shortcuts import render_to_response
    from rango.models import Category
    from rango.models import Page
    from rango.forms import CategoryForm
    from django.shortcuts import render

    def show_category(request, category_name_slug):
        context_dict = {}
        try:

            category = Category.objects.get(slug=category_name_slug)

            pages = Page.objects.filter(category=category)

            context_dict['pages'] = pages

            context_dict['category'] = category
        except Category.DoesNotExist:

            context_dict['category'] = None
            context_dict['pages'] = None

        return render(request, 'rango/category.html', context_dict)

index view

   def index(request):
    context = RequestContext(request)

    category_list = Category.objects.order_by('-likes')[:5]
    page_list = Page.objects.all()

    context_dict = {'categories':category_list, 'pages': page_list}

    for category in category_list:
        category.url = category.name.replace(' ', '_')

    return render_to_response('rango/index.html', context_dict, context)

models.py

from django.db import models
from django.template.defaultfilters import slugify


class Category(models.Model):
    name = models.CharField(max_length=128, unique=True)
    views = models.IntegerField(default=0)
    likes = models.IntegerField(default=0)
    slug = models.SlugField(unique=True)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(Category, self).save(*args, **kwargs)

    class Meta:
        verbose_name_plural = 'Categories'

    def __unicode__(self):
        return self.name

    def __str__(self):
        return self.name
class Page(models.Model):
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=128)
    url = models.URLField()
    views = models.IntegerField(default=0)
    def __unicode__(self):
        return self.title

    def __str__(self):
        return self.title

category.html

<!DOCTYPE html>
 <html>
 <head>
 <title>Rango</title>
 </head>
 <body>
 <div>
 {% if category %}
 <h1>{{ category.name }}</h1>
 {% if pages %}
 <ul>
 {% for page in pages %}
 <li><a href="{{ page.url }}">{{ page.title }}</a></li>
 {% endfor %}
 </ul>
   <strong>Would you like to add more </strong>
            <a href="{% url 'add_page' category.slug %}">pages</a>
            <strong>?</strong>
 {% else %}
 <strong>No pages currently in category.</strong>
 {% endif %}
 {% else %}
 The specified category does not exist!
 {% endif %}
 </div>
 </body>
 </html>

index.html

<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
<title>Rango</title>
</head>
<body>
<h1>Rango says...hello world!</h1>
<h2>Most viewed Categories!</h2>
{% if categories %}
<ul>
{% for category in categories %}
<li><a href="/rango/category/{{ category.slug }}">{{ category.name }}</a></li>
{% endfor %}
</ul>
{% else %}
<strong>There are no categories present.</strong>
{% endif %}
<a href="/rango/add_category/">Add a New Category</a>

<h2>Most Viewed Pages!</h2>
{% if pages %}
<ul>
    {% for page in pages %}
    <li><a href="/rango/category/{{ category.url }}/{{ page.url }}">{{ page.title }}</a> </li>
    {% endfor %}
</ul>
{% else %}
<strong>There are no pages present!</strong>
{% endif %}
<a href="/rango/about/">About</a><br />
<img src="{% static 'rango.jpg' %}" alt="Picture of Rango" />
</body>
</html>

1 Answers1

1

Change this line in your index.html,

<a href="/rango/category/{{ category.slug }}">{{ category.name }}</a></li>

to,

<a href="{% url 'show_category' category_name_slug=category.slug %}">{{ category.name }}</a></li>
zaidfazil
  • 9,017
  • 2
  • 24
  • 47
  • the initial http://127.0.0.1:8000/rango/ page is giving error now..it says NoReverseMatch at /rango/ Reverse for 'show_category' with arguments '('',)' not found. 1 pattern(s) tried: ['rango/category/(?P[\\w\\-]+)/$'] – Syed Zeeshan Jun 22 '17 at 11:05
  • Still similar error : NoReverseMatch at /rango/ Reverse for 'show_category' with keyword arguments '{'category_name_slug': ''}' not found. 1 pattern(s) tried: ['rango/category/(?P[\\w\\-]+)/$'] – Syed Zeeshan Jun 22 '17 at 12:10