what does {variable_name} in an url pattern mean in Django? - django

I'm reading Django Rest Framework documentation on router (https://www.django-rest-framework.org/api-guide/routers/) and encounter this:
^accounts/{pk}/$
I understand ^ means start of line, $ means end of line and everything but {pk}. What does it mean?

It means that an url pattern that includes a primary key, si valid. In the case of the documentation you are consulting, it means that the following url pattern is valid: accounts/121/. What SimpleRouter will do with it, is that urls that contain a primary key ({pk}), will be used to update or delete the accounts record with such primary key. In the example, account with primary key '121' would be retrieved, updated or deleted through such url.
This "variable_name" is received as a parameter by the view method or class method.

Related

reversing admin "change" url

I´m trying to find some example of how to use change url of modelAdmin and what exactly result it gives. Sincerly I do not understand the part from docs:
"This will find the first registered instance of the admin application (whatever the instance name), and resolve to the view for changing poll.Choice instances in that instance."
You can reverse with:
reverse('app:poll_choice_change', kwargs={'object_id': pk})
With app the default namespace (you can specify a different one if you include this in the path(…) where you attach the admin.urls. poll the name of the app, choice the name of the model in lowercase, and pk the primary key of the object.

Django Rest API Url Pattern to handle . (dot) symbol

Creating Django REST API, Need suggestions to handle the .(dot char) in the urlpatterns. Below is the example detail:
I have a Model (test) with name as one of the fields and name value is of format ABC.XYZ
Below URL pattern does not work when name = ABC.XYZ
url(r'^tests/(?P<string>[\w\-]+)/$', views.tests.as_view(), name='api_tests_name')
You can add the dot to the character group in the regex:
url(r'^tests/(?P<string>[\w\-.]+)/$', views.tests.as_view(), name='api_tests_name')
So now you make a request with tests/foo.bar/ as path for example.

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'),

Mapping urls in routes.py

I am using web2py and builing a REST api and have one of my URLs set up like this:
routes_in (
('/myapp/something/{?P<id>.*)/myfunction', /myapp/default/myfunction/\g<id>')
)
routes_out = (
('/myapp/default/myfunction/\g<id>', '/myapp/something/{?P<id>.*)/myfunction')
)
If my app is setup this way my function is not even entered into and I get an invalid request if I remove the id argument from the url that my url is being mapped to i.e. remove g<id> from above, I enter my function but the argument is not being captured.
I cannot change the structure of the URL as per my requirements and I am not sure how to go about this.
I would appreciate any pointers.
Thanks,
nav
The above does work in web2py I found that some other area of my code was breaking.

How to make case insensitive queries with Django Models

I have an member model with contains an email field. I recently realized that if a part of the email is capitalized, it won't show up in Django queries if I try to filter by the email (multiple member objects have the same email, but it may not be capitalized). I could have just made all emails lower-case when entering them into the database, but it's too late for that now (as the website is already launched). So how do I check who has a certain email, without being case sensitive?
Just use iexact:
User.objects.filter(email__iexact='email#email.com')
Case-insensitive exact match. If the value provided for comparison is None, it will be interpreted as an SQL NULL (see isnull for more details).
Member.objects.filter(email__iexact=email)