Forums

URL redirect not working

hi i have urls.py in my app file:

from django.conf.urls.defaults import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns



urlpatterns = patterns('project.appname.views',
    url(r'^picture$','picture'),
    url(r'^$', 'index'),
)
urlpatterns += staticfiles_urlpatterns()

Views looks like this:

def index(request):
    ..........
        return redirect('/picture')

def picture(request):
  .......
    return render(request,'picture.html', content)

it goes to appname.pythonanywhere.com showing index, and when it should redirect, it goes to /pictures, but uses index.html. not picture.html..... It works perfectly on my computer, but not in pythonanywhere page...

Hi there -- I think I'd need to look at your code to debug this, do you mind if I do? I can access it from within PythonAnywhere with your permission. We can move the discussion to email if there's stuff in there you'd rather not talk about on the public forums.

No problem You may access and share here what ever bugs you find :)

OK, I think what's happening is that all of your views are going to the index. In your main project/urls.py file you have this rule:

url(r'.*', include('project.widelook.urls')),

And then in project/widelook/urls.py you have these:

url(r'^/picture','picture'),
url(r'^$', 'index'),

The problem, I think, is that the rule in project/urls.py is "eating" all of the URL path, so by the time the rules in project/widelook/urls.py are processed, there's no path to match -- so the request is always handled by the second empty rule.

You can see this if you try going to any random URL on your site -- http://mihkell.pythonanywhere.com/admin just gives you the index page, as does http://mihkell.pythonanywhere.com/somethingthatdefinitelyshouldnotbehere.

The fix is to change the rule in project/urls.py to not consume the URL path:

url(r'^', include('project.widelook.urls')),

You can see the difference in the regular expression at the start.

Give that a go and let me know if it helps.

url(r'^', include('project.widelook.urls')),

and project.widelook.urls

url(r'^picture$','picture'),

Soleved it. Thanks