2

Trying to pass Business Name in URL in stead of ID. When I pass IDs, everything is fine.

urls.py

url(r'^(?P<name>\w+)/$', 'views.business'),

views.py

def business(request, name=1):
    return render_to_response('business.html', 
                             {'business': business.objects.get(name=name) })

template.html

<a href="http://website.com/{{ business.name|slugify }}/">Name{{ business.name }}</a>

When I do this, it will only work for single word business name such as "Bank" however if the business has multiple words "Wells Fargo" it will not work.

My goal is to use slugify to pass short SEO friendly URL such as

http://website.com/business-name/

Thanks for your time and for your help!

WayBehind
  • 1,607
  • 3
  • 39
  • 58

2 Answers2

1

Accordint to re module docs \w:

matches any alphanumeric character and the underscore

and the url you are trying to match has a dash because django's slugify method converts spaces and some non-ascii chars into dashes. So the fix consists in modifying the urls.py pattern to:

url(r'^(?P<name>[\w-]+)/$', 'views.business'),

But this isn't enough. Your current view will try to get a Business instance with the slugified name and will throw a DoesNotExists exception. So you should do one of the folowing things:

  • Add an slug field to your Business model which value must be slugify(business.name)

  • or add an id to the url, like this:

    url(r'^(?P[\w-]+)/(?P\d+)/$', 'views.business'),

and modify your view to get the instance by id:

def business(request, name, obj_id):
    return render_to_response('business.html', {'business': business.objects.get(id=obj_id) })
matagus
  • 6,136
  • 2
  • 26
  • 39
  • Same as above. Query error: I guess the query cant match the name with the hyphen in it. DoesNotExist at /wells-fargo/ Business matching query does not exist. Request Method: GET Request URL: http://127.0.0.1:8000/wells-fargo/ Django Version: 1.5.4 Exception Type: DoesNotExist Exception Value: Business matching query does not exist. Thanks for the help either way! – WayBehind Mar 07 '14 at 02:49
  • right, I edited my answer to fix that. Hope it helps. – matagus Mar 07 '14 at 02:59
  • Thank you for the help! Some great ideas and even though temporarily I went with the above, I'm planning to add the slugify(business-name) to the Models so we can better control the URL values. Thanks again! – WayBehind Mar 07 '14 at 03:25
0

First of all, you need to allow dashes in your url configuration:

url(r'^(?P<name>[-\w]+)/$', 'views.business'),

[-\w]+ matches "alphanumeric" characters in any case, underscore (_) and a dash.

Also, in the view, you need to "unslugify" the value passed in:

def business(request, name='unknown'):
    name = name.replace('-', ' ').capitalize()
    return render_to_response('business.html', 
                             {'business': business.objects.get(name=name) })

Also see:

Hope that helps.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Query error: I guess the query cant match the name with the hyphen in it. DoesNotExist at /wells-fargo/ Business matching query does not exist. Request Method: GET Request URL: http://127.0.0.1:8000/wells-fargo/ Django Version: 1.5.4 Exception Type: DoesNotExist Exception Value: Business matching query does not exist. – WayBehind Mar 07 '14 at 02:48
  • Perhaps the view should have some kind of replace('-', ' ') function to remove the hyphens from the URL to match the name in DB? – WayBehind Mar 07 '14 at 02:52