如何通过URL传递带空格的变量:Django

时间:2022-08-24 12:16:52

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

我通过url传递带有空格的变量时遇到了麻烦。现在假设我有一个对象

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.

现在为信息亭输入的“名称”是“Akash Deshpande”并保存。现在,当重定向到视图中的新页面时,我正在使用“信息亭名称”,即

 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

主要的urls.py只是通过“信息亭”将所有内容指向urll 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.

现在,不是转到此页面,而是发出错误“找不到页面”。我如何传递其中有空格的变量?问题清楚了吗?任何帮助将受到高度赞赏。

2 个解决方案

#1


9  

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.

而对于Pete的爱,请使用reverse()。它会帮助你捕捉这样的愚蠢错误。

#2


1  

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


9  

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.

而对于Pete的爱,请使用reverse()。它会帮助你捕捉这样的愚蠢错误。

#2


1  

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'),