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.
Related
I am using UpdateView for my update page, but I get this error:
Field 'None' expected a number but got 'update'.
my urls.py looks like this:
path('post/<pk>/update/', PostUpdateView.as_view(), name='post_update'),
Everything works fine if I change my update URL to anything other than "post". For example, this works fine:
path('abcxyz/<pk>/update/', PostUpdateView.as_view(), name='post_update'),
I would like to use the word "post" in URL, because i also use it in the other URLs and they work just fine (except for delete view, which gives me the same error). Is there any way to fix this error without changing the URL?
Note: It has worked before, but it broke at some point when I was trying to use django-PWA. I'm not sure if this is related though.
Thank you for your help!
This is likely because you have another path like:
path('post/update/update/', PostUpdateView.as_view(), name='post_update'),
or something similar. It is possible that one or both update/s are URL parameters.
If you then for example visit post/update/update, and your PostUpdateView is defined first, the it will trigger the view with pk='update'.
If the primary key is simply a number, you can make use of the <int:…> path converter to prevent triggering the view when pk is something else than a sequence of digits:
path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post_update'),
EDIT: Based on your comment, there was another view before this one with:
path('post/<year>/<month>/', FilteredPostListView.as_view(), name='filtered_post')
If one thus visits post/123/update, it will trigger the FilteredPostListView with 123 as year, and update as month, and hence it will raise an error.
You can put the path below that of the ProductUpdateListView, and you furthermore might want to use the <int:…> as well to only trigger the view when both parameters are sequences of digits:
path('post/<int:year>/<int:month>/', FilteredPostListView.as_view(), name='filtered_post')
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)
...
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
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)
I'm developing application using Bottle. How do I get full query string when I get a GET Request.
I dont want to catch using individual parameters like:
param_a = request.GET.get("a","")
as I dont want to fix number of parameters in the URL.
How to get full query string of requested url
You can use the attribute request.query_string to get the whole query string.
Use request.query or request.query.getall(key) if you have more than one value for a single key.
For eg., request.query.a will return you the param_a you wanted. request.query.b will return the parameter for b and so on.
If you only want the query string alone, you can use #halex's answer.