如何通过URL中的空格传递变量:Django [英] How to pass variables with spaces through URL in :Django
问题描述
我在通过URL传递变量的空格时遇到麻烦。
现在假设我有一个对象
class Kiosks(models.Model):
name = models。 CharField(max_length = 200,unique = True)
owner = models.ForeignKey(User)
现在,为信息亭输入的名字是Akash Deshpande并保存。现在在重定向到视图中的新页面时,我使用kiosk name
ie
消息。成功(请求,Kiosk已成功编辑)
返回HttpResponseRedirect('/ kiosks /'+ kiosk.name +'/')
满足此网址的视图如下:
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})
在urls.py中只需使用kiosks将所有内容指向urls
kiosks urls.py
urlpatterns = patterns 'kiosks.views',url(r'^(\w +)/ $','dashboard'),)
现在,而不是去这个页面,它发出错误找不到页面。我如何传递有空格的变量?
问题清楚吗?
任何帮助将非常感激。
允许正则表达式中的空格。
code> urlpatterns = patterns('kiosks.views',url(r'^([\w] +)/ $','dashboard'),)
/ pre>
对于Pete的爱,使用
reverse()
。这将有助于您抓住这样的愚蠢的错误。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.
解决方案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.这篇关于如何通过URL中的空格传递变量:Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!