URL not resolving correctly in Django URL - django

I just tried the following in my AJAX update:
[Server]/secTypes/Update
This maps to the following url in URLS.py:
url(r'^secTypes/Update/', equity.views.updateSecTypes, name='updateSecTypes'),
This doesn't resolve to the following function in my view.
But when I change the URL expression to:
url(r'^su/', equity.views.updateSecTypes, name='updateSecTypes')
It works fine.
What in the URL resolver is not getting accurately mapped? Is it the forward slash?
I think it has to do with something related to the regex so if someone understands this better can help me that would be appreciated.

From the url patterns in your comments, it looks like you had another matching pattern before the one in your question.
There are two simple solution for this.
Move that first pattern down. Change this:
url(r'^secTypes/', equity.views.getSecTypes, name='getSecTypes'),
url(r'^secTypesAll/', equity.views.getSecTypesAll, name='getSecTypesAll'),
url(r'^secTypes/Update/', equity.views.updateSecTypes, name='updateSecTypes'),
url(r'^secTypes/Delete/', equity.views.deleteSecTypes, name='deleteSecTypes'),
url(r'^secTypes/Create/', equity.views.createSecTypes, name='createSecTypes'),
to this:
url(r'^secTypesAll/', equity.views.getSecTypesAll, name='getSecTypesAll'),
url(r'^secTypes/Update/', equity.views.updateSecTypes, name='updateSecTypes'),
url(r'^secTypes/Delete/', equity.views.deleteSecTypes, name='deleteSecTypes'),
url(r'^secTypes/Create/', equity.views.createSecTypes, name='createSecTypes'),
url(r'^secTypes/', equity.views.getSecTypes, name='getSecTypes'),
The order matters when resolving URL patterns and if an earlier one matches, the following ones are not processed.
Both r'^secTypes/' and r'^secTypes/Update/' matches the string 'secTypes/Update/' so you need to be careful to put the more specific one first and the more general one afterwards.
Update the regex to match the end of the URL string by adding a $ like this:
url(r'^secTypes/$', equity.views.getSecTypes, name='getSecTypes'),
url(r'^secTypesAll/$', equity.views.getSecTypesAll, name='getSecTypesAll'),
url(r'^secTypes/Update/$', equity.views.updateSecTypes, name='updateSecTypes'),
url(r'^secTypes/Delete/$', equity.views.deleteSecTypes, name='deleteSecTypes'),
url(r'^secTypes/Create/$', equity.views.createSecTypes, name='createSecTypes'),
This is the preferred solution since it would stop Django from matching a URL like secTypes/Update/foobar
However, if you have logic in the view that specifically uses the substring after the end of the URL pattern (i.e. foobar based on the above example), this wouldn't work.

Related

How to use variable at start of django url to return to view?

I am trying to pass the first part of a django url to a view, so I can filter my results by the term in the url.
Looking at the documentation, it seems quite straightforward.
However, I have the following urls.py
url('<colcat>/collection/(?P<name>[\w\-]+)$', views.collection_detail, name='collection_detail'),
url('<colcat>/', views.collection_view, name='collection_view'),
In this case, I want to be able to go to /living and have living be passed to my view so that I can use it to filter by.
When trying this however, no matter what url I put it isn't being matched, and I get an error saying the address I put in could not be matched to any urls.
What am I missing?
<colcat> is not a valid regex. You need to use the same format as you have for name.
url('(?P<colcat>[\w\-]+)/collection/(?P<name>[\w\-]+)$', views.collection_detail, name='collection_detail'),
url('(?P<colcat>[\w\-]+)/$', views.collection_view, name='collection_view'),
Alternatively, use the new path form which will be much simpler:
path('<str:colcat>/collection/<str:name>', views.collection_detail, name='collection_detail'),
path('<str:colcat>/', views.collection_view, name='collection_view'),

Django URL pattern with combination of words and numbers in any order

I am trying to build a URL pattern in Django that can allow the following variations:
example:
folder/9/
folder/word/
folder/word_word/
folder/word_word1098/
folder/word-word/
folder/word-word-9/
folder/wo3rd/
folder/w/
or perhaps I should just allow any char/digit for that second position like:
folder/[anything_in_any_anyorder_here]/
any suggestions?
This worked for me:
/*([a]|[^a])*/

Regular expression for validating url with parameters

I have been searching high and low for a solution to this, but to no avail. I am trying to prevent users from entering poorly formed URLs. Currently I have this regular expression in place:
^(http|https)\://.*$
This does a check to make sure the user is using http or https in the URL. However I need to go a step further and validate the structure of the URL.
For example this URL: http://mytest.com/?=test is clearly invalid as the parameter is not specified. All of the regular expressions that I've found on the web return valid when I use this URL.
I've been using this site to test the expressions that I've been finding.
Look I think the best solution for testing the URL as :
var url="http://mytest.com/?=test";
Make 2 steps :
1- test only URL as :
http://mytest.com/
use pattern :
var pattern1= "^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)([A-Za-z]){2,3}(\/)?";
2- split URL string by using pattern1 to get the URL query string and IF URL has Query string then make test on It again by using the following pattern :
var query=url.split(pattern1);
var q_str = query[1];
var pattern2 = "^(\?)?([0-9A-Za-z]+=[0-9A-Za-z]+(\&)?)+$";
Good Luck,
I believe the problem you are having comes from the fact that what is or is not a valid parameter from a query string is not universally defined. And specifically for your problem, the criteria for a valid query is still not well defined from your single example of what should fail.
To be precise, check this out RFC3986#3.4
Maybe you can make up a criteria for what should be an "acceptable" query string and from that you can get an answer. ;)

Django: How can I make a part of the URL optional

I have a url in which I would like to make the status token optional. If the status token is not provided in the url I give a default value in the view method argument. I tried replacing the token with this (?:/(?P<status>\d+))?$ but doesn't seems to work well. Thanks
url(r'^(?P<status>\d+)/$', frequest_list, name="frequest_list"),
def request_list(request, status=1):
...
...
Update:
This was the pattern I was trying:
url(r'^(?:/(?P<status>\d+))?$', frequest_list, name="frequest_list"),
So, if I try localhost/features/ works well
But if I do localhost/features/1/ it fails
Just create a second url entry that calls the same view:
url(r'^features/$', frequest_list, name="frequest_list_default"),
url(r'^features/(?P<status>\d+)/$', frequest_list, name="frequest_list"),
I use single url optional captures in some of my projects, and they work fine. You might want to adjust your pattern to make the trailing / optional. I think that is what is causing your url to not match. Django does have an "APPEND_SLASH" settings bool that will add that on to your urls if they are missing it and don't match:
url(r'^features(?:/(?P<status>\d+))?/?$', frequest_list, name="frequest_list")
The optional / could probably also be written like this:
url(r'^features/?(?:(?P<status>\d+)/?)?$', frequest_list, name="frequest_list")

disallow some domain names, allow others

For example there are URLs http://www.subdomain1.domain.com.uk and http://www.subdomain2.domain.uk, from these URLs I need to extract only the name subdomain1 or subdomain2.
But if I receive http://www.subdomain3.co.uk or http://www.subdomain4.com I need to get the whole URL like subdomain3.co.uk or subdomain4.com.
My expression: ^http:\/\/(?:www\.)?((?!SubdomainToNegate|www).*)((?!\.domain\.com\.uk|\.domain\.uk).*)$
Expression catches whole URL.
My situation is shown better over there: http://www.rubular.com/r/B1iOUoUq33
^http:\/\/(?:www\.)?((?!SubdomainToNegate|www)[^\.]*)((?!\.domain\.com\.uk|\.domain\.uk).*)$
difference is this
(?!SubdomainToNegate|www)[^\.]*
instead of this
(?!SubdomainToNegate|www)\.*
finally found solution:
http:\/\/(?:www\.)?((?:(?!domain.com.uk|domain.uk)[^\s.]+)(?:\.(?!domain.com.uk|domain.uk)[^\s.]+)*)