Django: Reverse not found > {% url %} in template - django

This problem seems simple and it is described multiple times in SO, but I still can't figure out why it isn't working in my case.
So, I have a url declared in urls.py
urlpatterns = patterns('',
url(r'^(?P<country>[-\w]+)/$', CountryListView.as_view(), name='list_by_country'),)
and in my template I'm calling the url
<a href="{% url 'list_by_country' country.country__name %}" >{{ country.country__name }}</a>
However, I am getting the error message that the url could not be reversed
Reverse for 'list_by_country' with arguments '(u'United Kingdom',)' and keyword arguments '{}' not found
What is causing the reverse error? Are spaces in the argument maybe not allowed?

The problem is that "United Kingdom" doesn't match the regex [-\w]+. If you also want to match spaces, you should change your regex to [-\w\ ]+. Example:
url(r'^(?P<country>[-\w\ ]+)/$', CountryListView.as_view(), name='list_by_country')
You can also choose to match \s instead of just a single space character.

Related

Why django is not accepting spaces in template?

{% %} means that this is a django template delimiter. So inside {% %} the space should not matter I guess.
Now in the example:
{{ post.title }}
This is showing expected output whereas
{{ post.title }}
is showing error.
Why this is sensitive to whitespace?
Django template tags allow you to have positional and named parameters, so, if you have a space, it will assume that pk Is the first argument, = the seccond and so on.
So, if you you are passing named parameters, you have to use = without spaces, and if you are using positional parameters, you don't use = and separate them just by a space.
Because django templates are text processed and this is the syntax, you could try the same thing in bash, this is off-topic, but it is processed the same way that statements are sensitive to whitespace.
valid in bash
declare TEST=SOMETHING # is valid
invalid in bash
declare TEST = SOMETHING # is invalid
(same as in django template language)
If your question was Why this is sensitive to whitespace? than the answer is Because of syntax. Some languages are case sensitive some are case insensitive, in some you must prefix every variable with dollar ($). Thats only the syntax matter. Everything is processed and if you want your pc to understand what is happening, than it must be formally expressed with correct syntax.
There are Django docs: https://docs.djangoproject.com/en/2.0/topics/templates/#syntax

How to Match only url from a tag node js

I have a tag <span style="color: rgb(255,255,255);">[1]</span>
I am using this regex <a href="(.*)">(.*)<\/a> But its not parsing the url only. Its also parsing <span style="color: rgb(255,255,255);">[1]</span>
How can i get only the url from a tags?
Easily! Capture everything that is between href, that is a key word, and <.
href=(.*?)>
If you don't want to capture "", try this one:
href="(.*?)">
Although I am not much experienced with node.js, I think this one may work, but it won't be hard for you if you know the Regex.
var pattern = new RegExp(/href="(.*?)">/);
Here is Regex101.

How to find out if part of the string is in quotes?

So I have this pattern:
{% url my_view %}
{% url my_view user_id %}
And this is bad, so instead it should be like this:
{% url 'my_view' %}
{% url 'my_view' user_id %}
So the 'my_view' part should always be in quotes. All I need to do Is I need to find all the occurrences where 'my_view' part is not in quotes.
How can I do it?
Use lookarounds:
(?<={% url )[^'\s]*(?!')
See it in action
You can replace the result of following regex :
/\{% url (\S+)(.*)%\}/
With :
/\{% url \'\1\'\2%\}/
Or in some regex engines you may need to use $1 instead of \1.
You can use the following to match:
(?<=\{% url )(\S+)
And replace with:
'$1'
See DEMO
Edit: If you already have some my_view's with quotes use the following:
(?<=\{% url )([^']\S+[^'])(?=\s)
See DEMO

Regex for matching url prefix

I want to delete the Google prefix in all URLs.
<a href="http://news.google.com/news/url?sa=t&fd=R&ct2=en&usg=YFo&url=http://www.goo.tv/gd/2015/0509/735557.html
dfgdfgdfgdfgdf9
<a href="http://news.google.com/news/url?sa=t&fd=R&ct2=en&usg=AFQjCNFUS_UVkd9L-r7g&clid=c3878e0698331&cid=5213281008&ei=5DFNVJ4eymQLmyYFo&url=http://www.goo.tv/gd/2015/0509/735557.html
I want to remove http://news.google.com/news/url?sa=t&fd=R&ct2=en&blalba....url=
this Google prefix, so that it only retains the real URL.
I tried the regex, but it doesn't match each prefix, it matches all content
<a href="(http:\/\/news.google.com/news/url\?([\s\S]*)&url=)
Use Lazy Quantifiers:
<a href="(http:\/\/news.google.com\/news\/url\?([\s\S]*?)&url=)
Your regex did not worked because it was greedy(*), and took the match until the last &url= found. Lazy quantifiers(*?) stops at first match found, which is the expected behavior for your case.

Balanced regular expression

so I got my hands on regular expressions and tried to match the outer {% tag xyz %}{% endtag %} tags of the following text using regular expressions:
{% tag xyz %}
{% tag abc %}
{% endtag %}
{% endtag %}
My regular expression looks as follows and works so far:
({%)\s*(tag)([^%}]*?)(?:\s*(?:(%})((?:(?:[^{%]*?)|(?R))*)(?:({%)\s*(end\2)\s*(%}))))
But whenever the text inside of the matching tags contains a single { or % sign, the regex won't work as expected. I think it's because of the character classes that may match {% but also { or % as single characters too. I tried a lot and ended up with try and error but without success.
Any help on that issue?
I setup two regex101 links for you to show the issue:
works: https://regex101.com/r/qH0rI5/1
does not work: https://regex101.com/r/qH0rI5/2
Any help is really appreciated!
Try to to replace [^{%] with (?:(?!{%).) and add the s (PCRE_DOTALL) flag:
This would allow { that are not followed by % in between by using a negative lookahead.
Test your updated pattern or here another start to try with:
/{% tag \w+ %}(?:(?:(?!{%).)|(?0))*{% endtag %}/gs
test at regex101