How to pass variables with spaces through URL to: Django

I am having problems passing variables with spaces to them through 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 the kiosk is called "Akash Deshpande" and saved. Now, going to a new page in the views, I use the "kiosk name", that is.

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

The view that satisfies 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 just directs everything with the help of "kiosks" to pick up urls.py kiosks

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

Now, instead of going to this page, the message "Page not found" is displayed. How to pass variables that have space? Is the question clear? Any help would be greatly appreciated.

+7
source share
2 answers

Allow spaces in regex.

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

And for love of Pete, use reverse() . This will help you catch stupid mistakes like this.

+9
source

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'), 
+1
source

All Articles