从Django中的URL获取变量中的两个字符串 [英] Getting two strings in variable from URL in Django

查看:614
本文介绍了从Django中的URL获取变量中的两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的urls.py如下:

  urlpatterns = patterns('',
url(r'^ rss /(?P< anything> [^ /] +)/ $' 'rss.rssama.views.makerss',name ='anything'),
url(r'^ $','rss.rssama.views.home'),

views.py

  def maakrss(request,anything):

所以现在需要从www.mydomain.com/rss / [任何东西] /发送任何东西给我的看法。但是我也希望它将另一个字符串发送到views.py,如:



www.mydomain.com/rss/ [anynumber] / [anystring] /



我尝试过这个但是没有工作:

  url '^ rss /(?P< anynumber> [^ /] +)/(?P< anystring> [^ /] +)/ $','rss.rssama.views.makerss',name ='anynumber',name2 ='anystring'),

但这不行,它给出了这个错误:重复的关键字参数(urls.py,第17行)



所以我的问题:如何让它从url中提供两个字符串? p>

解决方案

首先,正则表达式部分应如下所示:

  r'^ / rss /(?P< anynumber> \d +)/(?P< anystring>。+)/ $'
/ pre>

< ...> 部分中的这些字符串允许您给任何名称正则表达式匹配。然后,Django将使用该名称将该值传递给您的函数。因此,您的函数必须具有相同名称的参数。在这种情况下,Django将使用 anynumber 的值,并使用该值作为函数的参数,名为 anynumber 。对于 anystring 也是如此,这个系统可以让你不必担心你的函数的参数是什么顺序。



\d + 将匹配一个或多个数字字符(数字)。限制正则​​表达式只匹配数字,如果这是你打算捕获的,而不是任何字符,希望只有数字出现。如果你想将数字部分限制在一定数量的数字,你可以使用 \d {1,4} 从一到四位数字。



下一部分(?P< anystring>。+)将捕获由一个或多个任意字符组成的字符串。这实际上会匹配像'letters / moreletters',包括斜杠。 Python正则表达式中有许多特殊序列可能有帮助。要仅匹配数字,字母和下划线字符,请使用 \w ,如(?P< anystring> \w +)。要更加宽松但忽略空白或任何其他无意义的(?P< anystring> [a-zA-Z1-9:; _ {} \ [\]] 来捕捉一大堆角色,确保在正则表达式中避免任何可能是一个特殊字符的东西,但是要保守,如果允许太多的选项可以知道你需要解决什么样的错误



现在进入 url 函数的name参数,该名称不会通过捕获的模式对于你的函数,它是一个特定类的调用视图函数的名称,可以在其他上下文中使用,比如模板标签 {%url view-name arg1 arg2%所以,你已经有了这个名字,任何东西,是指对你的view函数的调用,传递一个恰好被称为任何东西的关键字参数,对于你要传递的情况两个字符串,给出一个名称,如rss-number-string来表示你想要的参数,或一个名字这是指您的视图将使用该组合执行的特殊功能。



我一直使用相同功能的多个名称,其关键是: p>

  def makers(request,anystring = None,anynumber = None):

通过给出参数默认值,它允许您以不同的方式使用相同的功能。在这种情况下,当您只想传递 anystring 的值时,或当 anystring anynumber 应该有值。



我知道这是很多不同点,所以我会尝试这一切都在一起,所以你可以看到它可以如何工作。要有两个URL,一个捕获一个字符串并传递它,另一个捕获一个数字,一个斜杠,然后一个字符串,但都指向相同的视图功能,你可以使用这个:

  urlpatterns = patterns('',
url(r'^ rss /(?P< anystring> \w +)/ $' 'rss.rssama.views.makerss',name ='rss-anystring'),
url(r'^ rss /(?P< anynumber> \d +)/(?P< anystring> \w + )/ $','rss.rssama.views.makerss',name ='rss-number-string'),
url(r'^ $','rss.rssama.views.home'),

具有这样的视图功能:

  def makers(request,anystring = None,anynumber = None):
if anystring:
if anynumber:
#Do something字符串和数字
else:
#Do只有字符串



<请让我知道这是否有帮助。另外,Django的岩石,所以kudos!



Python Regex Library Docs


I'm having some trouble sending along more than one variable to the view.

my urls.py is as follows:

urlpatterns = patterns('',
    url(r'^rss/(?P<anything>[^/]+)/$', 'rss.rssama.views.makerss', name='anything'),
    url(r'^$', 'rss.rssama.views.home'),    
)

views.py

def maakrss(request, anything):

So now it takes from www.mydomain.com/rss/[anything]/ and sends 'anything' to my view. However I also want it to send along another string to views.py, like:

www.mydomain.com/rss/[anynumber]/[anystring]/

I tried this but that didn't work:

url(r'^rss/(?P<anynumber>[^/]+)/(?P<anystring>[^/]+)/$', 'rss.rssama.views.makerss', name='anynumber', name2='anystring'),

But this doesn't work, it gives this error: keyword argument repeated (urls.py, line 17)

So my question: How can I make it to give along two string from the url?

解决方案

To begin with, the regex part should look like this:

r'^/rss/(?P<anynumber>\d+)/(?P<anystring>.+)/$'

Those strings inside the <...> parts allow you to give a name to whatever the regex matches. Django will then use that name to pass the value to your function. Therefore your function must have an argument with the same name. In this case, Django will take the value called anynumber and use that value for the parameter of your function that is called anynumber. The same goes for anystring, and this system frees you from worrying about what order the arguments of your function are in.

\d+ will match one or more numeric characters (digits). It's good practice to limit the regex to match only numbers if that's what you intend to catch, rather than any character and hope that only numbers appear. If you wanted to limit the digits part to a certain number of digits, you could use \d{1,4} to take from one to four digits.

The next part, (?P<anystring>.+) will catch a string consisting of one or more of any characters. This would actually match something like 'letters/moreletters', including the slash. There are a number of "special sequences" in Python regex that might help. To match only digits, letters, and the underscore character, use \w, as in (?P<anystring>\w+). To be more lax but ignore whitespace or any other non-sense, (?P<anystring>[a-zA-Z1-9:;_{}\[\]] to catch a whole slew of characters. Make sure to escape anything that might be a special character in a regex. However, be conservative. If you allow too many options who knows what sorts of bugs you'll have to work out later.

Now onto name parameter of the url function. That name is not what it will pass the caught patterns to your functions as. It's a name for a particular class of invocation of your view function that can be used as a short-hand in other contexts like, the template tag {% url view-name arg1 arg2 %}. So, the name you have already, "anything", refers to a call to your view function, passing it one keyword argument that happens to be called anything. For the case where you want to pass two strings, give that a name like "rss-number-string" to signify the arguments you want to take, or a name that refers to the special function your view will be performing with that combination.

I use multiple names for the same function all the time, and the key is this:

def makerss(request, anystring=None, anynumber=None):

By giving the parameters default values, it allows you to use the same function in different ways. In this case, the function can be used when you only want to pass a value for anystring, or when anystring and anynumber should have values.

I know this is a lot of different points, so I'll try to put it all together so you can see how it might work. To have two urls, one which catch a string and passes it on, and another which catches a number, a slash, and then a string, but both point to the same view function, you could use this:

urlpatterns = patterns('',
    url(r'^rss/(?P<anystring>\w+)/$', 'rss.rssama.views.makerss', name='rss-anystring'),
    url(r'^rss/(?P<anynumber>\d+)/(?P<anystring>\w+)/$', 'rss.rssama.views.makerss', name='rss-number-string'),
    url(r'^$', 'rss.rssama.views.home'),    
)

With a view function something like this:

def makerss(request, anystring=None, anynumber=None):
    if anystring:
        if anynumber:
            #Do something with the string and the number
        else:
            #Do something with just the string

Please let me know if this helps. Also, Django rocks, so kudos!

Python Regex Library Docs

这篇关于从Django中的URL获取变量中的两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆