hi i have url like this:
path('api/v1/store/download/<str:ix>/', DownloadVideoAPI.as_view(), name='download'),
it accept long string .
I want to keep allthing after download key in above URL as the parameter.
but when I enter a long string that contains some slash Django says page not found for example when if enter "/api/v1/store/download/asdasd2asdsadas/asdasd" will give me 404 not found ...
how can I do that?
this is my view:
class DownloadVideoAPI(APIView):
def get(self, request, ix):
pre = ix.split(",")
hash = pre[0]
dec = pre[1]
de_hash = decode_data(hash, dec)
Well, It's possible to add the extra parameters in the request. you can use re_path method.
# urls.py
from django.urls import re_path
re_path(r'api/v1/store/download/(?P<ix>\w+)/', DownloadVideoAPI.as_view(), name='download'),
ref: https://docs.djangoproject.com/en/2.0/ref/urls/#django.urls.re_path
Just use
path('api/v1/store/download/<str:ix>', DownloadVideoAPI.as_view(), name='download'),
without / at the end.
/api/v1/store/download/asdasd2asdsadas/asdasd will result in a 404 page since Django cannot map the URL, /api/v1/store/download/asdasd2asdsadas/, to a route in your urls.py. To solve this, aside from using BugHunter's answer, you could URL encode your long string first before passing it to your URL.
So, given the long string, "asdasd2asdsadas/asdasd", URL encode it first to "asdasd2asdsadas%2Fasdasd". Once you have encoded it, your URL should now look like "/api/v1/store/download/asdasd2asdsadas%2Fasdasd".
To URL encode in Python 3, you can use urllib.
import urllib
parameter = 'asdasd2asdsadas/asdasd'
encoded_string = urllib.quote(parameter, safe='')
encoded_string here should have the value, "asdasd2asdsadas%2Fasdasd".
Related
i have this url
path('<slug>/thank_you/<user_id>', thank_you, name='thank_you'),
i want the <user_id> to be optional, but i dont want to make 2 urls like this
path('<slug>/thank_you', thank_you, name='thank_you'),
path('<slug>/thank_you/<user_id>', thank_you, name='thank_you2'),
i understand that you can make it optional using regex, but thats if you're using django <2 (using url, not path)
how do i obtain this ?
You can use URL Query String for this. For example:
# URL
path('/thank_you/', thank_you, name='thank_you'),
# View
def thank_you(request, slug):
user_id = request.GET.get('from')
# rest of the code
# Example route
http://localhost:8000/dummy-slug/thank_you/?from=dummy_user_id
I am passing date from my template to Django view. I am using jquery-ui datepicker. The format I see on console is like this.
If I send '05-01-2014' the format is like '05%2F01%2F2014'
In my view I have
dataFrom = request.GET.get('dataFrom', 'default')
dataTo = request.GET.get('dateTo', 'default')
results.filter(date__range=[dataFrom,dataTo ])
but I am not getting any result.My question is how to correctly format date so I get the result?
The URL and its parameters are encoded, you need to decode them to get back characters:
>>> import urllib
>>> urllib.unquote('05%2F01%2F2014')
'05/01/2014'
# Python 3
>> from urllib.parse import unquote
>>> unquote('05%2F01%2F2014')
'05/01/2014'
As you can see, you have slashes here instead of dash -, you might need to transform this to get a valid queryset.
From what I can see there are 2 ways of using a string from within the URL. Either by using the URLConf or via request.GET.get.
Im trying to use the request.GET.get method and trying to capture the string after the = sign on a URL such as www.domain.co.uk/macoui=001100
URLConf
url(r'^macoui=\d{6}$', 'domain.views.output'),
View
def output(request):
if request.method == 'GET':
request.GET.get('macoui', '')
return render_to_response('template.html', {'response': '\'%s\' % (macoui)}, context_instance=RequestContext(request))
When I run this I get an error saying "nothing to repeat".
Thanks,
You have quite a few errors here.
You've correctly stated that there are two ways of getting parameters, then confused them both. If you're matching things in the urlconf, then you don't use request.GET to get them. Anyway, request.GET is only for querystring parameters - that is, URLs of the form www.domain.co.uk/?param=value where the ? is the necessary part.
If your URL is really www.domain.co.uk/macoui=001100 - without the ? - then you need to fix your regular expression as follows:
url(r'^macoui=(?P<macaoui>\d{6})$', 'domain.views.output'),
and your view as follows:
def output(request, macaoui):
However, if - as is much more likely - your URL is www.domain.co.uk/?macoui=001100 - with a question mark - then your URLconf should just be:
url(r'^$', 'domain.views.output'),
and your view becomes:
def output(request):
macaoui = request.GET.get('macoui', '')
I believe you have a problem in this line:
{'response': '\'%s\' % (macoui)}
As you're escaping the last apostrophe and the entire string is never closed. You'll be better off using the other string delimiters:
{'response': "'%s'" % (macoui)}
Good luck.
You should simply do.
url(r'^/.*', 'domain.views.output'),
then use URL, www.domain.co.uk/?macoui=001100
URL on your example what not proper as get parameters (request.GET) on request uri are followed after '?'
I would like to know how can I encode a URL so that I can pass it as a parameter into google maps?
https://maps.google.com/maps?q=https:%2F%2Fdl.dropbox.com%2Fu%2F94943007%2Ffile.kml&hl=en&sll=32.824552,-117.108978&sspn=0.889745,1.575165&t=m&z=4
as you can see the URL is being passed as a parameter into the maps.google.com/ url
the question is if i start with a url like http://www.microsoft.com/somemap.kml, how can i encode this URL so that i can pass it into it
The process is called URL encoding:
>>> urllib.quote('https://dl.dropbox.com/u/94943007/file.kml', '')
'https%3A%2F%2Fdl.dropbox.com%2Fu%2F94943007%2Ffile.kml'
Hi how to use python to transform the url of a article to it's print url.
article url:http://www.indianexpress.com/news/second-time-as-farce/800228/0
print url:http://www.indianexpress.com/story-print/800228/
How to convert article url to print url?
Use urllib.parse.urlparse() to carve the path from the rest of the url, and posixpath.split() and posixpath.join() to reform the path, and urllib.parse.urlunparse() to put it all back together again.
from urllib.parse import urlparse
def transform(url):
parsed = urlparse(url)
return '{0}://{1}/story-print/{2}/'.format(parsed.scheme, parsed.netloc, parsed.path.split('/')[-2])