0

When I go to http://localhost:8000/getAndAnalyzePosts/index it 404s and adds a trailing slash (it does this regardless of whether I have APPEND_SLASH = True or APPEND_SLASH = False

Going to http://localhost:8000/getAndAnalyzePosts/test doesn't work but http://localhost:8000/getAndAnalyzePosts/test.anything does.

I really want to use '^$' for the index but that isn't working either.

I have this app in a mezzanine project, I haven't tried putting it into a regular django project - probably should do that next. The rest of my project works fine (using mezzanine's default apps)

getAndAnalyzePosts/urls.py

from django.conf.urls import patterns, url

from getAndAnalyzePosts import views

urlpatterns = patterns('',
    url(r'^index$', views.index, name='index'),
    url(r'^test.+$', views.test, name='test'),
    url(r'^getSentiment$', views.getSentiment, name='getSentiment'),
)

note: getSentiment wants post variables so I am not really testing that directly

urls.py (main project)

from __future__ import unicode_literals

from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from mezzanine.core.views import direct_to_template
admin.autodiscover()
urlpatterns = i18n_patterns("",
    ("^admin/", include(admin.site.urls)),
)
urlpatterns += patterns('',
    url(r"^$", direct_to_template, {"template": "index.html"}, name="home"),
    (r"^", include("mezzanine.urls")),
    url(r'^getAndAnalyzePosts/', include('getAndAnalyzePosts.urls', namespace="getAndAnalyzePosts")),
)
handler404 = "mezzanine.core.views.page_not_found"
handler500 = "mezzanine.core.views.server_error"

Also, I run python manage.py show_urls from django-extensions and it returns:

/getAndAnalyzePosts/getSentiment        getAndAnalyzePosts.views.getSentiment   getSentiment
/getAndAnalyzePosts/index      getAndAnalyzePosts.views.index  index
/getAndAnalyzePosts/test.+      getAndAnalyzePosts.views.test   test
exrhizo
  • 141
  • 2
  • 13

2 Answers2

1

The project urls.py you've pasted here at some stage had some comments in great big capital letters describing the exact problem you're facing, here's their original source:

https://github.com/stephenmcd/mezzanine/blob/master/mezzanine/project_template/urls.py#L63-L66

Steve
  • 1,726
  • 10
  • 16
0

Append slash option makes sure that if the incoming url does not have trailing slash and no url gets match, then it retires after adding trailing slash. What it does not do is retry after removing trailing slash from incoming url. Since incoming url does have a trailing slash, you need a / before $ Add / at the end, before $ in all your urls, (r'^getSentiment/$) Let append slash be true and it should work.

.+ works because . in regex matching anything except newline and you added + after it so it matches any character any number of times, to match . exactly you need to escape it (.) Making it ^text\..+$

lmc
  • 420
  • 2
  • 11
  • I tried `^index/$` with `APPEND_SLASH = True` in my settings and this 404'd when I went to `http://localhost:8000/getAndAnalyzePosts/index/` – exrhizo Feb 13 '14 at 02:14
  • Is your debug=True? can you give the response returned on your browser by django? – lmc Feb 13 '14 at 02:22
  • It isn't giving any details only says 404, request method GET, and request URL and "You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page." – exrhizo Feb 13 '14 at 02:25
  • Nope cannot reproduce it in same config. Has your show_urls output changed since the change? – lmc Feb 13 '14 at 02:49
  • It added the / /getAndAnalyzePosts/getSentiment getAndAnalyzePosts.views.getSentiment getSentiment /getAndAnalyzePosts/index/ getAndAnalyzePosts.views.index index /getAndAnalyzePosts/test.+ getAndAnalyzePosts.views.test test – exrhizo Feb 13 '14 at 03:02
  • Do you by any chance have something like this: `url(r'^getAndAnalyzePosts/', include('somethingelse.urls', namespace="getAndAnalyzePosts")), url(r'^getAndAnalyzePosts/', include('getAndAnalyzePosts.urls', namespace="getAndAnalyzePosts")),)` That way it will show urls in show_url option but still give 404 – lmc Feb 13 '14 at 03:05
  • I put the full urls.py for thee main project – exrhizo Feb 13 '14 at 06:32