-1

I've been trying to implement hyper links into my Django application, where a list of items are displayed, clicking on each item will take you to a page detailing more information about the item.

I've been wrestling with the {% URL %} tag and despite searching over here, the internet and books on the matter, I've yet to get it working.

In views.py:

def Link(request):
    return render_to_response('Search_Page.html')

In Urls.py:

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'ParkManager.views.home', name='home'),
    # url(r'^ParkManager/', include('ParkManager.foo.urls')),
    url(r'^test/', Search_Page),
    url(r'^search/', Search),
    url(r'^details/', Details_Main),
    url(r'^Link/(d+}/$', Link),
    url(r'^$', 'Parks.views.Link', name="home"),

in my template:

 <a href="{% url home %}">test</a>

Thanks for your time :)

EDIT error:

The page loads however the link only takes you to 127 .0 .0 .1 /8000 when I add: test

I get:

NoReverseMatch at /search/
Reverse for 'name' with arguments '(u'North West Thrill Centre',)' and keyword arguments '{}' not found.


Request Method:

GET



Request URL:

http://127.0.0.1:8000/search/?search=a&type=parks&submit=Search



Django Version:

1.4.2



Exception Type:

NoReverseMatch



Exception Value:

Reverse for 'name' with arguments '(u'North West Thrill Centre',)' and keyword arguments '{}' not found. 


Exception Location:

C:\Python27\lib\site-packages\django\template\defaulttags.py in render, line 424



Python Executable:

C:\Python27\python.exe



Python Version:

2.7.3



Python Path:

['C:\\Users\\User\\Documents\\Django\\ParkManager',
 'C:\\Windows\\system32\\python27.zip',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\plat-win',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages'] 


Server time:

Mon, 4 Feb 2013 16:05:30 +0000



Error during template rendering

In template C:\Users\User\Documents\Django\ParkManager\Templates\Details_Main.html, error at line 23

Reverse for 'name' with arguments '(u'North West Thrill Centre',)' and keyword arguments '{}' not found.
4

2 に答える 2

4

A clue: Exception Location:
C:\Python27\lib\re.py in _compile, line 242

Your issue is not related to the url tag. It is a mal-formed regex in your urls.py.

urls.py

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'ParkManager.views.home', name='home'),
    # url(r'^ParkManager/', include('ParkManager.foo.urls')),
    url(r'^test/', Search_Page),
    url(r'^search/', Search),
    url(r'^details/', Details_Main),
    # LINE BELOW has an open parentheses and not a closed parentheses. 
    url(r'^Link/(d+}/$', Link), 
    #url(r'^Link/(d+)/$', Link), #line fixed 
    url(r'^$', 'Parks.views.Link', name="home"),
于 2013-02-04T15:44:33.473 に答える
1

unbalanced parenthesis is problem at this line:

url(r'^Link/(d+}/$', Link),

You have forgotten to close the parenthesis.

If you are using {% url %} tag in Django < 1.5, use it this way:

{% load url from future %}

{% url 'namespace:viewname' arg1, arg2 %}
{% url 'namespace:viewname' kwarg1=val, kwarg2=val2 %}

If you are using Django 1.5, you don't have to load the special url tag. If you are not using namespaces (good if you are using general view names like list, detail etc. and you want to distinguish between apps, e.g.: author:list or book:list) use only the view name. Check the documentation, there is a good section about the url tag - https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#url

于 2013-02-04T15:51:36.810 に答える