Uncommon django urlpattern - django

Suppose I've got urls on my site which look like /qwe/asd/zxc/wer/sdf/xcv/ (unknown number of \w+ blocks separated by slashes).
Is that possible? to construct a urlpattern to catch those urls so that:
a view function would get all those \w+ strings in *args list to it
the {% url qwe asd zxc wer sdf xcv %} templatetag would reconstruct supposed /qwe/asd/zxc/wer/sdf/xcv/ url the right way
Thanks

For No.1 (r'^(?P<url>[\w/]+)$', 'views.handler') could do that.

Related

Why my Django URL doesn't grab the right way?

I've a problem with django url, when I go to:
/cars/my_town/my_office/
It's ok and view run as expected, town="my_town" and office_name="my_office".
When I go to:
/car/my_town/my_office/my_var1/
I get an error.
When I print the vars in my views I get:
town :"my_town/my_office" and office_name:"my_var1"
My view looks like this:
def ListingCars(request,town,office_name,var1=None) :
My urls:
url(r'^cars/(?P<town>[\w|\W ]+)/(?P<office_name>[\w|\W ]+)/$', web_public_views.ListingCars, name='listingvo_cars'),
url(r'^cars/(?P<town>[\w|\W ]+)/(?P<office_name>[\w|\W ]+)/(?P<var1>[\w|\W ]+)/$', web_public_views.ListingCars, name='listingvo_cars_var1'),
SOLVE ... it wasn't a resolverurl problem, but a myfault problem ;) i didn't see a "รง" on my tags name url... that cause the problem... thx for help i clean up my regex and sorry
\W is not word, which will match a slash, you can most likely just omit that and use \w, although its not clear what your url's should match, if it is slugs then its most likely that you need [\w-]+
^cars/(?P<town>[\w-]+)/(?P<office_name>[\w-]+)/$
^cars/(?P<town>[\w-]+)/(?P<office_name>[\w-]+)/(?P<var1>[\w-]+)/$
What is actually happening is your first regex is matching your url so it never uses the second one, so most likely you could also fix this by switching the order of these urls so the second is first but I wouldn't recommend this.

Error NoReverseMatch on Reverse Django URL

I have declared an url like following in django 1.7:
url(r'^page(/\w{2}/|/)$', MyView.as_view(), name='my_name'),
In my template, I want to reverse the url from its name. I tried:
<form method="post" action="{% url 'my_namespace:my_name' variable %}">
<form method="post" action="{% url 'my_namespace:my_name' %}">
But nothing works, it throwed exception:
Reverse for 'my_name' with arguments '(u'test',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['page(\/\w{2}\/|\/)$']
Please help by giving me some advice. Thank you.
The regex appears incorrect for matching the argument 'test'.
If we take a look at the regex (/\w{2}/|/),
It appears to be capturing two groups which are separated by an 'OR' (|) operator.
The first group is /\w{2}/. This is only going to match '/' followed by \w only 2 times followed by '/'.
The second group is only matching '/'.
'test' doesn't match either one of these cases so it raises an exception.
I believe the correct regex we are looking to capture here would be,
(\w+)
The correct URL structure would look like this,
url(r'^page/(\w+)/$', MyView.as_view(), name='my_name')
url(r'^page/$', MyView.as_view(), name='my_name')
This would match given the argument 'test' because the regex (\w+) says match any of the characters in this group [a-zA-Z0-9_] one or more times and each character in 'test' falls into this category.

Regex match except urls starting with a custome strings

I have a text and a regex pattern
text is something like
foo https://www.google.hu <img ... src="http://a-page.com/foobar.jpg" ...> bar
the regex
/(http|https|ftp)\:\/\/(www\.)?([a-zA-Z0-9\-\_\.]+)\.([a-z]{1,5}+)\/([a-zA-Z0-9\.\?\=\&\-\_\~\/\%\+\;]+)?(\#([a-zA-Z0-9\_]+))?/i
and i'd update it with a special case
if url starting with src=" it would be great if regex matches dont contains the image url only other urls
i tried this
/(?!src\=\")(http|https|ftp)\:\/\/(www\.)?([a-zA-Z0-9\-\_\.]+)\.([a-z]{1,5}+)\/([a-zA-Z0-9\.\?\=\&\-\_\~\/\%\+\;]+)?(\#([a-zA-Z0-9\_]+))?/
but it doesnt work
Could you help me, please?
I know I could add (^|\s) to pattern, but it won't work in case when I want to hide urls cause user can write any char before url and the url is no longer hidden and some other regex codes are in source too and one of them is a img bb tag code, and I dont want to hide (replace) it's url
(Sorry for my english)
To be honest I had difficulties to understand what exactly you want, but I guess you mean that you have a text with various URLs inside and you don't want to match those which are included in a html img tag. If so, try this:
/(?<!src\=\")(https?|ftp):\/\/(www\.)?([\w\-\.]+)\.([a-z]{1,5}+)\/?([\w\.\?\=\&\-\~\/\%\+\;]+)?(\#(\w+))?/
Notes:
You can replace [A-Za-z0-9_] with character class \w (read more in perlre).
The (?!pattern) assertion you tried is a negative look-ahead assertion. In your case you want a negative look-behind (?<!pattern) (again you can read perlre for more info).

how to get value of url parameter with colon in text in django

I have the following url:
http://mysite.com/config/1:1/
This gives me a 404 page not found.
If I try this: it find my url entry no problem.
http://mysite.com/config/1/
This what my url pattern looks like:
url(r'^config/(?P<config_id>\d+)/$', views.config, name='config'),
Is there a problem with having the colon in the url?
Your regular expression allows only digits. \d doesn't match :. If your view function is able to handle the colon, broaden the pattern:
url(r'^config/(?P<config_id>[\d:]+)/$', views.config, name='config')
Since you've named the pattern config_id I suspect it's a primary key, and this will not work with the view, but that depends on the view itself.

How to pass a url as a parameter to a handler in Django?

A project I'm currently working on has a sort of proxy functionality where users can browse to another URL through the site. I was hoping the URL could be something like this:
www.mydomain.com/browser/[URL here]
However I'm having trouble capturing a URL as a parameter like this. I think my inexperience with regex is failing me here :( My URL conf looks like this:
urlpatterns += patterns('',
url(r'^browser/(?P<url>\w+)/$', browser_proxy, name='browser_proxy'),
)
I'm thinking the \w+ isn't sufficient to capture an arbitrary URL. Anyone know what I should be using here to capture a URL as a parameter like this?
Thanks for any help.
\w means a "word" character, i.e. alphanumeric and underscore. It is equivalent to the set [a-zA-Z0-9_]. You can match any character with a period. I.e.:
urlpatterns += patterns('',
url(r'^browser/(?P.+)/$', browser_proxy, name='browser_proxy'),
)