Beau McDowell
Beau McDowell

Reputation: 19

python - Django : Page Not found

I have looked around and can't really find a solution to my problem. Here is the error django throws. This error is being thrown when on my homepage I have a fiew links that upon clicking should direct you to a details view of said link.

Using the URLconf defined in untitled.urls, Django tried these URL patterns, in this order:

    ^$
    ^index/ ^$ [name='index']
    ^index/ ^(?P<distro_id>[0-9]+)/$ [name='distro_id'] 
    ^admin/

The current URL, index//, didn't match any of these.

To my knowledge I don't understand why this error is being thrown.

Here is my urls.py

from django.conf.urls import include, url
from django.contrib import admin
import index.views
urlpatterns = [
    url(r'^$', index.views.index),
    url(r'^index/', include('index.urls', namespace='index')),
    url(r'^admin/', admin.site.urls),
]

My index/urls.py

from django.conf.urls import url
from . import views


urlpatterns = [
    # /index/
    url(r'^$', views.index, name='index'),

    #/distro/123/
    url(r'^(?P<distro_id>[0-9]+)/$', views.detail, name='distro_id'),

]

My views.py

from django.shortcuts import get_object_or_404, render
from django.template import loader, RequestContext
from django.http import Http404
from .models import Distros


def index(request):
    all_distros = Distros.objects.all()
    context = {'all_distros': all_distros, }
    return render(request, 'index/index.html', context)


def detail(request, distro_id,):
    distro_id = get_object_or_404 (Distros, pk=distro_id)
    return render(request, 'index/detail.html', {'distro_id': distro_id})

template code:

    {% extends 'index/base.html' %}

{% block body %}
<ul>
    {% for distro in all_distros %}
        <li><a href="/index/{{ distro_id }}/">{{ index.distro_id }}</a></li>
    {% endfor %}
</ul>
{% endblock %}

I believe those are all the relevent files. I believe everything is setup correctly so I am not sure why the error is being thrown. I'm sure im missing something simple that i'm just overlooking.

Upvotes: 2

Views: 205

Answers (1)

Selcuk
Selcuk

Reputation: 59184

Please don't use hardcoded URLs as they are error prone as in your situation. Instead of:

<a href="/index/{{ index.distro.id }}/">

use the url template tag with your namespace (index) and view name (distro_id):

<a href="{% url 'index:distro_id' index.id %}">

Note that you also have an error with index.distro.id as index is actually a Distros object. It has an id field, but not distro.id.

Upvotes: 3

Related Questions