To get started, part of the regex should look like this:
r'^/rss/(?P<anynumber>\d+)/(?P<anystring>.+)/$'
These lines inside the <...>
parts allow you to specify a name to match the regular expression. Django will then use this 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 of anynumber
and use this value for the parameter of your function, which is called anynumber
. The same goes for anystring
, and this system frees you from worrying about the order in which the arguments to your function are.
\d+
will match one or more numeric characters (digits). It is good practice to limit the regex to match only numbers, if that is what you intend to catch, not some character, and hope that only numbers appear. If you want to limit part of the digits to a certain number of digits, you can use \d{1,4}
to write one to four digits.
The next part (?P<anystring>.+)
catch a string of one or more characters. In fact, it will look like 'letters/moreletters'
, including a slash. Python-regex has a number of “special sequences” that can help. To match only numbers, letters, and the underscore, use \w
, as in (?P<anystring>\w+)
. To be weaker but to ignore the space or any other non-sense, (?P<anystring>[a-zA-Z1-9:;_{}\[\]]
to catch an integer number of characters. Make sure you Avoid anything that might be a special character in a regular expression, but be conservative if you allow too many options that know which errors you will have to develop later.
Now on the url
function name parameter. This name is not what will pass caught patterns to your functions. This is the name for a specific call class of your view function, which can be used as a short hand in other contexts, such as the template tag {% url view-name arg1 arg2 %}
. So, the name that you already have, “everything,” refers to calling your view function, passing it an argument with one keyword, which, as they say, is called something. For the case when you want to pass two strings, specify a name like "rss-number-string" to indicate the arguments you want to accept, or a name that refers to a special function that your representation will perform with this combination.
I use multiple names for the same function all the time, and the key is:
def makerss(request, anystring=None, anynumber=None):
By providing default parameter values, it allows you to use the same function in different ways. In this case, this function can be used when you want to pass a value for anystring
or when anystring
and anynumber
must have values.
I know that these are many different points, so I will try to put it all together so that you can see how this can work. To have two URLs, one that captures a string and passes it, and the other that catches a number, a slash, and then a string, but both point to the same browsing function, you can 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:
Please let me know if this helps. In addition, Django cliffs, so it is true!
Python regex library docs