tags:

views:

109

answers:

2

My URLconf has:

url(r'^view-item$', 'appname.views.view_item', name='view-item'),

Now, if I go to http://myhost/path_to_django_app/view-item/, it works. However, {% url view-item %} returns '/view-item/'. Why is it doing this?

This problem occurred when I moved the application to a new server, so I'm guessing something must be configured wrong, but I don't even know where to look.

+1  A: 

This may be due to the SCRIPT_NAME variable not being set correctly. The url tag will use this variable to compose the final absolute path to return.

You should check what request.META['SCRIPT_NAME'] is set to in one of your views. If it is not set correctly, then you may need to look into your backend configuration. If you are using mod_python, this usually involves making sure that django.root is set in the apache config. Check the installation docs for more info.

If you still can't get it to work, you can try adding this to settings.py:

FORCE_SCRIPT_NAME = '/path_to_django_app/'
sixthgear
+2  A: 

The usual way to write your URl in django is with a trailing '/':

url(r'^view-item/$', 'appname.views.view_item', name='view-item')

or:

url(r'^view-item/', include('view.urls')),

Life will be much easier if you follow this convention.

hughdbrown