Getting two lines of a variable from a URL in Django

I am having trouble sending more than one variable to a view.

my urls.py looks like this:

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 he takes from the site www.mydomain.com/rss/[anythingothing// and sends "everything" in my opinion. However, I also want it to send another line to views.py, for example:

www.mydomain.com/rss/[anynumberumber/[anystring.BIZ/

I tried this, but this did not work:

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

But this does not work, it gives this error: repeating the keyword argument (urls.py, line 17)

So my question is: how can I make it pass two strings from a URL?

+7
source share
3 answers

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: #Do something with the string and the number else: #Do something with just the string 

Please let me know if this helps. In addition, Django cliffs, so it is true!

Python regex library docs

+26
source

You do not need to give two name arguments for this. I mean, you already have the variable names inside the regular expression. The actual problem is that you cannot give two name arguments, so you can do this instead:

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

EDIT:

using urlConf above, you can create the corresponding view as:

 def makerss(request, anynumber, anystring): 
+1
source

What is name2 ? The url function takes a name parameter, which is the name of the URL when it is canceled, but you cannot add random extra functions.

Otherwise, you have the correct syntax for sending two elements to the view. Of course, since you masked the variable names and did not indicate the actual error or trace, we do not know what is actually going wrong.

0
source

All Articles