django : extract a path from a full url

  • Last Update :
  • Techknowledgy :

You can use urlparse module to extract the path:

try:
from urllib.parse
import urlparse # Python 3
except ImportError:
   from urlparse
import urlparse # Python 2

parsed = urlparse('http://stackoverflow.com/questions/32809595')
print(parsed.path)

Output:

'/questions/32809595'

Suggestion : 2

You can use urlparse module to extract anycodings_url the path:,I tried to build a HttpRequest from referer anycodings_django without success. Is there a class or type anycodings_django that I can use to easily extract the path anycodings_django from an URL?,Adding useMemo or useCallbacks to the map component breaks the code with error "Rendered more hooks than during the previous render ",TypeError: Converting circular structure to JSON --> starting at object with constructor 'ClientRequest'

Here is my code :

from django.core.urlresolvers
import resolve, Resolver404

#[...]

@register.simple_tag(takes_context = True)
def simple_tag_example(context):
   # The referer is a full path: http: //host:port/path/to/referer/
   # We only want the path: /path/to / referer /
   referer = context.request.META.get('HTTP_REFERER')
if referer is None:
   return ''

# Build the string http: //host:port/
   prefix = '%s://%s' % (context.request.scheme, context.request.get_host())
path = referer.replace(prefix, '')

resolvermatch = resolve(path)
# Do something very interesting with this resolvermatch...

You can use urlparse module to extract anycodings_url the path:

try:
from urllib.parse
import urlparse # Python 3
except ImportError:
   from urlparse
import urlparse # Python 2

parsed = urlparse('http://stackoverflow.com/questions/32809595')
print(parsed.path)

Output:

'/questions/32809595'

Suggestion : 3

path - Matches any non-empty string, including the path separator, '/'. This allows you to match against a complete URL path rather than a segment of a URL path as with str.,Another possibility is to include additional URL patterns by using a list of path() instances. For example, consider this URLconf:,This will include the nominated URL patterns into the given application namespace.,str - Matches any non-empty string, excluding the path separator, '/'. This is the default if a converter isn’t included in the expression.

from django.urls import path

from . import views

urlpatterns = [
path('articles/2003/', views.special_case_2003),
path('articles/<int:year>/', views.year_archive),
   path('articles/<int:year>/<int:month>/', views.month_archive),
         path('articles/<int:year>/<int:month>/<slug:slug>/', views.article_detail),
                  ]
class FourDigitYearConverter:
   regex = '[0-9]{4}'

def to_python(self, value):
   return int(value)

def to_url(self, value):
   return '%04d' % value
from django.urls import path, register_converter

from . import converters, views

register_converter(converters.FourDigitYearConverter, 'yyyy')

urlpatterns = [
path('articles/2003/', views.special_case_2003),
path('articles/<yyyy:year>/', views.year_archive),
   ...
   ]
from django.urls import path, re_path

from . import views

urlpatterns = [
    path('articles/2003/', views.special_case_2003),
    re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
    re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive),
    re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<slug>[\w-]+)/$', views.article_detail),
]
from django.urls import re_path

urlpatterns = [
re_path(r'^blog/(page-([0-9]+)/)?$', blog_articles), # bad
re_path(r'^comments/(?:page-(?P<page_number>[0-9]+)/)?$', comments), # good
   ]
# URLconf
from django.urls import path

from . import views

urlpatterns = [
path('blog/', views.page),
path('blog/page<int:num>/', views.page),
   ]

   # View (in blog/views.py)
   def page(request, num=1):
   # Output the appropriate page of blog entries, according to num.
   ...

Suggestion : 4

The easiest way is to put some check like this in code before redirect:,How to treat relative path as full URL in Django?,How to run script with Django settings in subdirectory without relative project path,Django Rest Framework: Modify Serializer to return dictionary using a field as the key instead of an array of objects

The easiest way is to put some check like this in code before redirect:

if not link.startswith('http//:www.'):
   link = 'http//:www.' + link

Suggestion : 5

Creating elaborate url parameters for django.urls.path statements requires exploring Django path converters. By default, Django is equipped with five path converters presented in table 2-3.,To relay this information a Django url path must treat this information as a parameter. Url parameters are declared differently for both django.urls.path and django.urls.re_path statements, with the former using Django path converters and the latter using standard Python regular expression named groups., Once you've set up url parameters using either django.urls.path statements with path converters or django.urls.re_path statements with regular expression named groups, you'll want to access them in a Django template or view method. Listing 2-7 illustrates two variations., A Django path converter class is a standard Python class that must comply with the presence of a class attribute named regex, as well as the class methods to_python() and to_url. Listing 2-5 illustrates two custom path converter classes, one to match roman numerals and the other to match float numbers.

path('drinks/<str:drink_name>/',...)
path('stores/<int:store_id>/',...)
path('about/<slug:page>/',...)
path('user/<uuid:user_id>/',...)
path('seo/<path:landing>/',...)

Suggestion : 6

This module defines a standard interface to break Uniform Resource Locator (URL) strings up in components (addressing scheme, network location, path etc.), to combine the components back into a URL string, and to convert a “relative URL” to an absolute URL given a “base URL.”,The urllib.parse module defines functions that fall into two broad categories: URL parsing and URL quoting. These are covered in detail in the following sections.,This Request For Comments includes the rules for joining an absolute and a relative URL, including a fair number of “Abnormal Examples” which govern the treatment of border cases.,Concrete class for urlparse() results containing str data. The encode() method returns a ParseResultBytes instance.

>>> from urllib.parse
import urlparse
   >>>
   urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme = 'scheme', netloc = 'netloc', path = '/path;parameters', params = '',
      query = 'query', fragment = 'fragment') >>>
   o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
      ..."highlight=params#url-parsing") >>>
   o
ParseResult(scheme = 'http', netloc = 'docs.python.org:80',
      path = '/3/library/urllib.parse.html', params = '',
      query = 'highlight=params', fragment = 'url-parsing') >>>
   o.scheme 'http' >>>
   o.netloc 'docs.python.org:80' >>>
   o.hostname 'docs.python.org' >>>
   o.port
80
   >>>
   o._replace(fragment = "").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
>>> from urllib.parse
import urlparse
   >>>
   urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
ParseResult(scheme = '', netloc = 'www.cwi.nl:80', path = '/%7Eguido/Python.html',
      params = '', query = '', fragment = '') >>>
   urlparse('www.cwi.nl/%7Eguido/Python.html')
ParseResult(scheme = '', netloc = '', path = 'www.cwi.nl/%7Eguido/Python.html',
      params = '', query = '', fragment = '') >>>
   urlparse('help/Python.html')
ParseResult(scheme = '', netloc = '', path = 'help/Python.html', params = '',
   query = '', fragment = '')
>>> from urllib.parse
import urlparse
   >>>
   u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html') >>>
   u
ParseResult(scheme = '', netloc = 'www.cwi.nl:80', path = '/%7Eguido/Python.html',
      params = '', query = '', fragment = '') >>>
   u._replace(scheme = 'http')
ParseResult(scheme = 'http', netloc = 'www.cwi.nl:80', path = '/%7Eguido/Python.html',
   params = '', query = '', fragment = '')
(addressing scheme, network location, path, query, fragment identifier).
>>> from urllib.parse
import urljoin
   >>>
   urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html')
'http://www.cwi.nl/%7Eguido/FAQ.html'
>>> urljoin('http://www.cwi.nl/%7Eguido/Python.html',
   ...'//www.python.org/%7Eguido')
'http://www.python.org/%7Eguido'

Suggestion : 7

On the other hand, in the case of the path, the “/” symbol is included and the whole path value is passed successfully.,You can see that the parameter value passed to the URL is shown in the Django template. If I change the value of the parameter, the result will also change:,path: This path converter consists of a non-empty string, including the “/” symbol, and is defined to pass a path as the parameter.,The difference between the str and the path is that if you pass a path parameter to an str converter, it will be terminated at the “/” symbol and you will not be able to send the full path.

In various web applications, you might have seen some URLs that end with some parameters. For example, look at the below URL:

https: //www.shop.tsinfo.com/products/12

But, firstly you have to create a path and map it to a view. For this, you have to edit your application’s urls.py file. A sample urls.py file will look like this:

from django.urls import path
from . import views
urlpatterns = [
path("URL endpoint">/<path converter: URL parmeter name>, view_name.function_name, name = "path name")
   ]

For example, if the requested URL is:

https: //www.shop.tsinfo.com/products/12

In the Django template, you can receive the parameter and display the results accordingly. For example, you can see in the below sample Django template how to fetch the parameter.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Products</title>
</head>
<body>
    <h2>Your parameter value is : {{URL parameter}}</h2>
</body>
</html>

For example, if the URL is:

http: //localhost:8000/products/125

Suggestion : 8

Last Updated : 07 Jan, 2021,GATE CS 2021 Syllabus

  • bs4: Beautiful Soup(bs4) is a Python library for pulling data out of HTML and XML files. This module does not come built-in with Python. To install this type the below command in the terminal.
pip install bs4
  • requests:  Requests allows you to send HTTP/1.1 requests extremely easily. This module also does not comes built-in with Python. To install this type the below command in the terminal.
pip install requests