Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass variables with spaces through URL in :Django

I am having trouble in passing variables with spaces in them through the urls. Now Suppose I have an object

class Kiosks(models.Model):
    name = models.CharField(max_length = 200, unique = True)
    owner = models.ForeignKey(User)

Now the "name" entered for kiosk is say "Akash Deshpande" and saved. Now while redirecting to a new page in the views, i am using the "kiosk name " i.e.

 messages.success(request,"Kiosk edited successfully") 
 return HttpResponseRedirect('/kiosks/'+kiosk.name+'/')

The view which caters to this url is as follows:

def dashboard(request, kiosk_name):
    kiosk =Kiosks.objects.get(name__iexact = kiosk_name)
    deal_form = DealsForm(kiosk=kiosk)
    code_form = CodeForm()
    unverified_transactions = get_unverified_transactions(kiosk)
    return render(request,'kiosks/dashboard.html',{'kiosk':kiosk, 
                                                   'deal_form' : deal_form,
                                                   'code_form' : code_form,
                                                   'unverified_transactions' : unverified_transactions})

The main urls.py simply directs everything with "kiosks" to bellow urls kiosks urls.py

urlpatterns = patterns('kiosks.views',url(r'^(\w+)/$', 'dashboard'),)

Now instead of going to this page it is giving an error "Page not found". How do i pass variables which have space in them ? Is the question clear? Any help will be highly appreciated.

like image 459
Akamad007 Avatar asked Nov 23 '11 06:11

Akamad007


People also ask

How do you handle whitespace in a URL?

URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

What is URL namespace in Django?

URL namespaces allow you to uniquely reverse named URL patterns even if different applications use the same URL names. It's a good practice for third-party apps to always use namespaced URLs (as we did in the tutorial). Similarly, it also allows you to reverse URLs if multiple instances of an application are deployed.

How Django URLs work with regular expressions?

We use regular expressions to match these url paths to their corresponding request handler (aka View). If a path, either dynamic or static, is matched the View will then handle the request and return a response to the User. If a path is not matched, Django will automatically send a 404 Page Not Found response.


2 Answers

Allow spaces in your regex.

urlpatterns = patterns('kiosks.views', url(r'^([\w ]+)/$', 'dashboard'),)

And for the love of Pete, use reverse(). It will help you catch silly mistakes like this.

like image 59
Ignacio Vazquez-Abrams Avatar answered Oct 21 '22 20:10

Ignacio Vazquez-Abrams


yup .. allow spaces in your regex .. something like this works for me ..

url(r'^find-interiordesigners/state-(?P<state>.+?)/$',DesignersByCategoryCityState.as_view(),name='findInterior-state'),
like image 27
vijay shanker Avatar answered Oct 21 '22 21:10

vijay shanker