QueryString in Django - python

I need one small help. I have one django application already with me and everything is working fine. Now requirement is without making change in existing functions we want to pass urls parameter as a query string
path('my-app/', include('myapp.urls', namespace='myapp')),
# I added this line because I want to use dcid as a querystring
url(r'^my-app/(?:(?P<dcid>\w+)/)',include('myapp.urls', namespace='myapp')),
and my function is already written like this
def orders_b2b_open_list_pageable(request):
We don't want to change in above functions but we want dcid in query string. How can we achieve that
I am doing this but when requesting like this
http://localhost:8002/my-app/1/orders/b2b/open/pageable/
I am getting following error
Thank You in advance

There's no need to parse it in your urls.py, that would change your function signiture.
You can handle it in your view, by pulling the querystring value from the request:
def orders_b2b_open_list_pageable(request):
dcid = request.GET.get('dcid', None)
...

Related

How to get the absolute string from a name-spaced Django URL?

You know how in a Django app, you can redirect to a different view using a simple function in your views? Its usually something like this:
return redirect('myapp:my_url')
The above redirect would redirect me to an absolute URL which could be something like this:
https://example.com/my-url/
Now, my question is, how can I get the https://example.com/my-url/ as a string using a function in my view? I don't want to redirect, I just want to get it, and save it in my variable.
So, by doing something like this:
print my_function('myapp:my_url')
We would get an output in the terminal like so:
https://example.com/my-url/
Any help? Thanks!
You can get the "path" element of the URL (ie /my-url/) using the reverse function you have already mentioned.
The domain of the website can be added using request.build_absolute_uri().
print(request.build_absolute_uri(reverse('myapp:my_url'))
Note that I'm using the Python 3 print syntax for this example.

How can I use multiple variables(paramaters) in a url in django

I can get the filler variable from the URL below just fine.
url(r'^production/(?P<filler>\w{7})/$', views.Filler.as_view()),
In my view I can retrieve the filler as expected. However, if I try to do use a URL like the one below.
url(r'^production/(?P<filler>)\w{7}/(?P<day>).*/$', views.CasesByDay.as_view()),
Both variables (filler, day) are blank.
You need to include the entire parameter in parenthesis. It looks like you did that in your first example but not the second.
Try:
url(r'^production/(?P<filler>\w{7})/(?P<day>.*)/$', views.CasesByDay.as_view()),
See the official documentation for more information and examples: URL dispatcher

Django: how to pass an entire URL as a parameter inside the app URL?

I am trying to pass an entire, full length/structure URL within my app as an argument inside the app's URL. For example, I want to be able to do something like:
myapp.com/https://www.youtube.com/watch?v=dQw4w9WgXcQ so that I can take the URL entered after my app's home page and store it. However, I think the app is getting confused when the URL pasted has fragments and query arguments (ie: contains # and/or ?) My urls.py looks like this:
url(r'^(?P<url_param>[a-zA-Z0-9_.-/:?=#]*)/$', views.anywebsiteentered, name='anywebsiteentered')
When I try to write a view that looks like below to take the inputted URL and save it inside a model object, I always get the URL truncated before the query and fragment characters, what can I do so that my application picks up the entire URL string?
def anywebsiteentered(request, url_param = 'url_param'):
UrlBlob.objects.create(fullurl=url_param)
For example, the above object created if my app is at myapp.com/https://www.youtube.com/watch?v=dQw4w9WgXcQ only returns https://www.youtube.com/watch and not the query part of the URL. I suspect it is something I am doing with the passing of the URL because when I create this model object manually inside the python-django shell there is no problems at all.
Thanks for any help and hints. I really appreciate it.
If you needed to use some url in "path" area of another url you should escape it's special characters. For example use %3F instead "?". It's called url escaping.
For your purpose would be better pass url as argument like:
myapp.com/?url=http://www.youtube.com/watch?v=dQw4w9WgXcQ
— browser will do necessary escaping in this case.
You can get the query string from request.META:
def anywebsiteentered(request, url_param='url_param'):
full_url = url_param
query_string = request.META['QUERY_STRING']
if query_string:
full_url += u'?' + query_string
UrlBlob.objects.create(fullurl=full_url)

Why won't my regex parse this URL in Django?

Currently working in Django, and I'm trying to set things up so that a form on one page calls a specific URL, for which the appropriate view is rendered. I'm having trouble with the regular expression that parses the URL, as it won't read the value '\?' as an escaped question mark, which is what I believe it should be doing. The following RE checks out on Pythex.
When the app submits the form, it calls the URL:
http://127.0.0.1:8000/map/?street=62+torrey+pines+cove&city=san+diego&state=CA&radius=50&drg=4
In my project level urls.py file, I have the following:
url(r'^map/', include('healthcare_search.urls', namespace="healthcare_search")),
This calls my app level urls.py file, where I have:
url(r'^\?street=(?P<street>[a-z0-9+]+)&city=(?P<city>[a-z+]+)&state=(?P<state>[a-z]{2})&radius=(?P<radius>[0-9]{1,3})&drg=(?P<drg>[0-9]{1,3})', views.map_hospitals, name = "map_hospitals"),
This just results in a 404 error, saying the URL doesn't match any of the patterns. I know that it's a RE problem, because I removed everything from the app level RE, and submitted just http://127.0.0.1:8000/map/ to see if it would call the right view, which it did successfully. Things seem to break apart on the '\?'. Any ideas what I'm doing wrong?
As a note, this is the first time I've written a regular expression, so my apologies if it is unclear or poorly written.
You don't want to get access to the variables that way. A better option is to get them from the request, since they'll be available in the request's dictionary of variables. In your view, you can get the value of street via request.GET.get('street', None), which will return the value if street is in the request or return None otherwise.

Pyramids route_url with additional query arguments

In Pyramids framework, functions route_path and route_url are used to generate urls from routes configuration. So, if I have route:
config.add_route('idea', 'ideas/{idea}')
I am able to generate the url for it using
request.route_url('idea', idea="great");
However, sometimes I may want to add additional get parameters to generate url like:
idea/great?sort=asc
How to do this?
I have tried
request.route_url('idea', idea='great', sort='asc')
But that didn't work.
You can add additional query arguments to url passing the _query dictionary
request.route_url('idea', idea='great', _query={'sort':'asc'})
If you are using Mako templates, _query={...} won't work; instead you need to do:
${request.route_url('idea', idea='great', _query=(('sort', 'asc'),))}
The tuple of 2-tuples works as a dictionary.

Categories

Resources