jmoneystl
jmoneystl

Reputation: 781

Django urlpatterns won't match

I have urlpatterns that aren't matching. This is something I'm adapting from the Django tutorial. It's probably something easy, but I'm just missing it.

The error:

Page not found (404)
    Request Method: GET
    Request URL:    http://127.0.0.1:8000/sendemail/1
Using the URLconf defined in emailme.urls, Django tried these URL patterns, in this order:
    1. ^sendemail ^$ [name='index']
    2. ^sendemail ^(?P<msg_id>\d+)/$ [name='detail']
    3. ^sendemail ^(?P<msg_id>\d+)/results/$ [name='results']
    4. ^admin/
The current URL, sendemail/1, didn't match any of these.

models.py:

from django.db import models

class Choice(models.Model):
    choice_text = models.CharField(max_length=200)

    def __unicode__(self):
        return self.choice_text

root urls.py:

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^sendemail', include('sendemail.urls')),
    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
)

urls.py:

from django.conf.urls import patterns, url
from sendemail import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^(?P<msg_id>\d+)/$', views.detail, name='detail'),
    url(r'^(?P<msg_id>\d+)/results/$', views.results, name='results'))

views.py:

from django.http import HttpResponse

def index(request):
    return HttpResponse("You're at the index")

def detail(request, msg_id):
    return HttpResponse("The details of message id %s" % msg_id)

def results(request, msg_id):
    return HttpResponse("The results of message id %s" % msg_id)

Upvotes: 0

Views: 252

Answers (1)

Peter DeGlopper
Peter DeGlopper

Reputation: 37344

This pattern, the one you're going for, requires a trailing slash.

url(r'^(?P<msg_id>\d+)/$', views.detail, name='detail'),

The URL you're using, sendemail/1, doesn't have one.

Upvotes: 1

Related Questions