{"id":75182,"date":"2020-01-18T12:22:50","date_gmt":"2020-01-18T06:52:50","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=75182"},"modified":"2024-04-12T14:17:11","modified_gmt":"2024-04-12T08:47:11","slug":"mapping-urls-to-views","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/","title":{"rendered":"Mapping URLs to Views"},"content":{"rendered":"<p>So, to recap, this view function returns an HTML page that includes the current date and time. But how do we tell Django to use this code? That\u2019s where URLconfs come in.<\/p>\n<p>If, at this point, you ran python manage.py runserver again, you\u2019d still see the \u201cWelcome to Django\u201d message, with no trace of our \u201cHello world\u201d view anywhere. That\u2019s because our mysite project doesn\u2019t yet know about the hello view; we need to tell Django explicitly that we\u2019re activating this view at a particular URL.<\/p>\n<p>Continuing our previous analogy of publishing static HTML files, at this point we\u2019ve created the HTML file but haven\u2019t uploaded it to a directory on the server yet. To hook a view function to a particular URL with Django, we use a URLconf.<\/p>\n<p>A URLconf is like a table of contents for your Django-powered web site. Basically, it\u2019s a mapping between URLs and the view functions that should be called for those URLs. It\u2019s how you tell Django, \u201cFor this URL, call this code, and for that URL, call that code.\u201d For example, when somebody visits the URL \/foo\/, call the view function foo_view(), which lives in the Python module views.py.<\/p>\n<p>When you executed django-admin startproject in the previous chapter, the script created a URLconf for you automatically: the file urls.py. By default, it looks something like this:<\/p>\n<p>&nbsp;<\/p>\n<pre>\"\"\"mysite URL Configuration\n\n&nbsp;The `urlpatterns` list routes URLs to views. For more information please see:&nbsp;&nbsp;\n\n&nbsp; https:\/\/docs.djangoproject.com\/en\/1.11\/topics\/http\/urls\/<\/pre>\n<p>Examples:<\/p>\n<pre>Function views&nbsp;&nbsp;&nbsp;\n\n 1. Add an import:&nbsp; from my_app import views&nbsp;&nbsp;&nbsp; \n\n2. Add a URL to urlpatterns:&nbsp; url(r'^\n<p>If we ignore the documentation comments at the top of the file, here\u2019s the essence of a URLconf:<\/p>\n<pre>from django.conf.urls import url\n\nfrom django.contrib import admin\n\nurlpatterns = [&nbsp;&nbsp;\n\n&nbsp;&nbsp; url(r'^admin\/', admin.site.urls),]<\/pre>\n<p>Let\u2019s step through this code one line at a time:<\/p>\n<ul>\n<li>The first line imports two functions from the django.conf.urls module: include which allows you to include a full Python import path to another URLconf module, and url which uses a regular expression to pattern match the URL in your browser to a module in your Django project.<\/li>\n<li>The second line calls the function admin from the django.contrib module. This function is called by the include function to load the URLs for the Django admin site.<\/li>\n<li>The third line is urlpatterns \u2013 a simple list of url() instances.<\/li>\n<\/ul>\n<p>The main thing to note here is the variable urlpatterns, which Django expects to find in your URLconf module. This variable defines the mapping between URLs and the code that handles those URLs. To add a URL and view to the URLconf, just add a mapping between a URL pattern and the view function. Here\u2019s how to hook in our hello view:<\/p>\n<pre>from django.conf.urls import url\n\nfrom django.contrib import admin\n\nfrom mysite.views import hello&nbsp;\n\nurlpatterns = [&nbsp;&nbsp;&nbsp;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url(r'^admin\/', admin.site.urls),&nbsp;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url(r'^hello\/\n<p>We made two changes here:<\/p>\n<ul>\n<li>First, we imported the hello view from its module \u2013 mysite\/views.py, which translates into mysite.views in Python import syntax. (This assumes mysite\/views.py is on your Python path.)<\/li>\n<li>Next, we added the line url(r'^hello\/$', hello), to urlpatterns. This line is referred to as a URLpattern. The url() function tells Django how to handle the URL that you are configuring. The first argument is a pattern-matching string (a regular expression; more on this in a bit) and the second argument is the view function to use for that pattern. url() can take other optional arguments as well.<\/li>\n<\/ul>\n<p>One more important detail we\u2019ve introduced here is that \u2018r\u2018 character in front of the regular expression string. This tells Python that the string is a \u201craw string\u201d \u2013 its contents should not interpret backslashes. In normal Python strings, backslashes are used for escaping special characters \u2013 such as in the string \u201c\\n\u201c, which is a one-character string containing a newline.<\/p>\n<p>When you add the r to make it a raw string, Python does not apply its backslash escaping \u2013 so, \u201cr'\\n'\u201d is a two-character string containing a literal backslash and a lowercase \u201cn\u201d. There\u2019s a natural collision between Python\u2019s usage of backslashes and the backslashes that are found in regular expressions, so it\u2019s best practice to use raw strings any time you\u2019re defining a regular expression in Django.<\/p>\n<p>In a nutshell, we just told Django that any request to the URL \/hello\/ should be handled by the hello view function. It\u2019s worth discussing the syntax of this URLpattern, as it may not be immediately obvious. Although we want to match the URL \/hello\/, the pattern looks a bit different than that. Here\u2019s why:<\/p>\n<ul>\n<li>Django removes the slash from the front of every incoming URL before it checks the URLpatterns. This means that our URLpattern doesn\u2019t include the leading slash in \/hello\/. At first, this may seem unintuitive, but this requirement simplifies things \u2013 such as the inclusion of URLconfs within other URLconfs.<\/li>\n<li>The pattern includes a caret (^) and a dollar sign ($). These are regular expression characters that have a special meaning: the caret means \u201crequire that the pattern matches the start of the string,\u201d and the dollar sign means \u201crequire that the pattern matches the end of the string.\u201d<\/li>\n<\/ul>\n<p>This concept is best explained by example. If we had instead used the pattern ^hello\/(without a dollar sign at the end), then any URL starting with \/hello\/ would match, such as \/hello\/foo and \/hello\/bar, not just \/hello\/.<\/p>\n<p>Similarly, if we had left off the initial caret character (i.e., hello\/$), Django would match any URL that ends with hello\/, such as \/foo\/bar\/hello\/. If we had simply used hello\/, without a caret or dollar sign, then any URL containing hello\/ would match, such as \/foo\/hello\/bar.Thus, we use both the caret and dollar sign to ensure that only the URL \/hello\/ matches \u2013 nothing more, nothing less.<\/p>\n<p>Most of your URLpatterns will start with carets and end with dollar signs, but it\u2019s nice to have the flexibility to perform more sophisticated matches. You may be wondering what happens if someone requests the URL \/hello (that is, without a trailing slash). Because our URLpattern requires a trailing slash, that URL would not match. However, by default, any request to a URL that doesn\u2019t match a URLpattern and doesn\u2019t end with a slash will be redirected to the same URL with a trailing slash (This is regulated by the APPEND_SLASH Django setting, which is covered in Appendix D).<\/p>\n<p>The other thing to note about this URLconf is that we\u2019ve passed the hello view function as an object without calling the function. This is a key feature of Python (and other dynamic languages): functions are first-class objects, which means you can pass them around just like any other variables. Cool stuff, eh?<\/p>\n<p>To test our changes to the URLconf, start the Django development server, by running the command python manage.py runserver from within your Python virtual environment. (If you left it running, that\u2019s fine, too. The development server automatically detects changes to your Python code and reloads as necessary, so you don\u2019t have to restart the server between changes.)<\/p>\n<p>The server is running at the address http:\/\/127.0.0.1:8000\/, so open up a web browser and go to http:\/\/127.0.0.1:8000\/hello\/. You should see the text \u201cHello world\u201d \u2013 the output of your Django view, as below<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-128514 aligncenter\" src=\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/1-380x214.jpg\" alt=\"\" width=\"380\" height=\"214\"><\/p>\n<h3>Regular Expressions<\/h3>\n<p>Regular expressions (or regexes) are a compact way of specifying patterns in text. While Django URLconfs allow arbitrary regexes for powerful URL-matching capability, you\u2019ll probably use only a few regex patterns in practice. Here\u2019s a small selection of common patterns:<\/p>\n<p>&nbsp;<\/p>\n<table style=\"height: 667px;\" width=\"617\">\n<thead>\n<tr>\n<td><strong>Symbol<\/strong><\/td>\n<td><strong>Matches<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>. (dot)<\/td>\n<td>Any character<\/td>\n<\/tr>\n<tr>\n<td>\\d<\/td>\n<td>Any digit<\/td>\n<\/tr>\n<tr>\n<td>[A-Z]<\/td>\n<td>Any character, A-Z (uppercase)<\/td>\n<\/tr>\n<tr>\n<td>[a-z]<\/td>\n<td>Any character, a-z (lowercase)<\/td>\n<\/tr>\n<tr>\n<td>[A-Za-z]<\/td>\n<td>Any character, a-z (case insensitive)<\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>One or more of the previous expression (e.g., \\d+ matches one or more digit)<\/td>\n<\/tr>\n<tr>\n<td>[^\/]+<\/td>\n<td>All characters except forward slash<\/td>\n<\/tr>\n<tr>\n<td>?<\/td>\n<td>Zero or more of the previous expression (e.g., \\d* matches zero or more digits)<\/td>\n<\/tr>\n<tr>\n<td>{1,3}<\/td>\n<td>Between one and three (inclusive) of the previous expression<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n, views.home, name='home')Class-based views&nbsp;&nbsp;&nbsp;\n\n 1. Add an import:&nbsp; from other_app.views import Home&nbsp;&nbsp;&nbsp; \n\n2. Add a URL to urlpatterns:&nbsp; url(r'^\n<p>If we ignore the documentation comments at the top of the file, here\u2019s the essence of a URLconf:<\/p>\n<pre wp-pre-tag-2=\"\"><\/pre>\n<p>Let\u2019s step through this code one line at a time:<\/p>\n<ul>\n<li>The first line imports two functions from the django.conf.urls module: include which allows you to include a full Python import path to another URLconf module, and url which uses a regular expression to pattern match the URL in your browser to a module in your Django project.<\/li>\n<li>The second line calls the function admin from the django.contrib module. This function is called by the include function to load the URLs for the Django admin site.<\/li>\n<li>The third line is urlpatterns \u2013 a simple list of url() instances.<\/li>\n<\/ul>\n<p>The main thing to note here is the variable urlpatterns, which Django expects to find in your URLconf module. This variable defines the mapping between URLs and the code that handles those URLs. To add a URL and view to the URLconf, just add a mapping between a URL pattern and the view function. Here\u2019s how to hook in our hello view:<\/p>\n<pre wp-pre-tag-3=\"\"><\/pre>\n<p>We made two changes here:<\/p>\n<ul>\n<li>First, we imported the hello view from its module \u2013 mysite\/views.py, which translates into mysite.views in Python import syntax. (This assumes mysite\/views.py is on your Python path.)<\/li>\n<li>Next, we added the line url(r'^hello\/$', hello), to urlpatterns. This line is referred to as a URLpattern. The url() function tells Django how to handle the URL that you are configuring. The first argument is a pattern-matching string (a regular expression; more on this in a bit) and the second argument is the view function to use for that pattern. url() can take other optional arguments as well.<\/li>\n<\/ul>\n<p>One more important detail we\u2019ve introduced here is that \u2018r\u2018 character in front of the regular expression string. This tells Python that the string is a \u201craw string\u201d \u2013 its contents should not interpret backslashes. In normal Python strings, backslashes are used for escaping special characters \u2013 such as in the string \u201c\\n\u201c, which is a one-character string containing a newline.<\/p>\n<p>When you add the r to make it a raw string, Python does not apply its backslash escaping \u2013 so, \u201cr'\\n'\u201d is a two-character string containing a literal backslash and a lowercase \u201cn\u201d. There\u2019s a natural collision between Python\u2019s usage of backslashes and the backslashes that are found in regular expressions, so it\u2019s best practice to use raw strings any time you\u2019re defining a regular expression in Django.<\/p>\n<p>In a nutshell, we just told Django that any request to the URL \/hello\/ should be handled by the hello view function. It\u2019s worth discussing the syntax of this URLpattern, as it may not be immediately obvious. Although we want to match the URL \/hello\/, the pattern looks a bit different than that. Here\u2019s why:<\/p>\n<ul>\n<li>Django removes the slash from the front of every incoming URL before it checks the URLpatterns. This means that our URLpattern doesn\u2019t include the leading slash in \/hello\/. At first, this may seem unintuitive, but this requirement simplifies things \u2013 such as the inclusion of URLconfs within other URLconfs.<\/li>\n<li>The pattern includes a caret (^) and a dollar sign ($). These are regular expression characters that have a special meaning: the caret means \u201crequire that the pattern matches the start of the string,\u201d and the dollar sign means \u201crequire that the pattern matches the end of the string.\u201d<\/li>\n<\/ul>\n<p>This concept is best explained by example. If we had instead used the pattern ^hello\/(without a dollar sign at the end), then any URL starting with \/hello\/ would match, such as \/hello\/foo and \/hello\/bar, not just \/hello\/.<\/p>\n<p>Similarly, if we had left off the initial caret character (i.e., hello\/$), Django would match any URL that ends with hello\/, such as \/foo\/bar\/hello\/. If we had simply used hello\/, without a caret or dollar sign, then any URL containing hello\/ would match, such as \/foo\/hello\/bar.Thus, we use both the caret and dollar sign to ensure that only the URL \/hello\/ matches \u2013 nothing more, nothing less.<\/p>\n<p>Most of your URLpatterns will start with carets and end with dollar signs, but it\u2019s nice to have the flexibility to perform more sophisticated matches. You may be wondering what happens if someone requests the URL \/hello (that is, without a trailing slash). Because our URLpattern requires a trailing slash, that URL would not match. However, by default, any request to a URL that doesn\u2019t match a URLpattern and doesn\u2019t end with a slash will be redirected to the same URL with a trailing slash (This is regulated by the APPEND_SLASH Django setting, which is covered in Appendix D).<\/p>\n<p>The other thing to note about this URLconf is that we\u2019ve passed the hello view function as an object without calling the function. This is a key feature of Python (and other dynamic languages): functions are first-class objects, which means you can pass them around just like any other variables. Cool stuff, eh?<\/p>\n<p>To test our changes to the URLconf, start the Django development server, by running the command python manage.py runserver from within your Python virtual environment. (If you left it running, that\u2019s fine, too. The development server automatically detects changes to your Python code and reloads as necessary, so you don\u2019t have to restart the server between changes.)<\/p>\n<p>The server is running at the address http:\/\/127.0.0.1:8000\/, so open up a web browser and go to http:\/\/127.0.0.1:8000\/hello\/. You should see the text \u201cHello world\u201d \u2013 the output of your Django view, as below<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-128514 aligncenter\" src=\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/1-380x214.jpg\" alt=\"\" width=\"380\" height=\"214\"><\/p>\n<h3>Regular Expressions<\/h3>\n<p>Regular expressions (or regexes) are a compact way of specifying patterns in text. While Django URLconfs allow arbitrary regexes for powerful URL-matching capability, you\u2019ll probably use only a few regex patterns in practice. Here\u2019s a small selection of common patterns:<\/p>\n<p>&nbsp;<\/p>\n<table style=\"height: 667px;\" width=\"617\">\n<thead>\n<tr>\n<td><strong>Symbol<\/strong><\/td>\n<td><strong>Matches<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>. (dot)<\/td>\n<td>Any character<\/td>\n<\/tr>\n<tr>\n<td>\\d<\/td>\n<td>Any digit<\/td>\n<\/tr>\n<tr>\n<td>[A-Z]<\/td>\n<td>Any character, A-Z (uppercase)<\/td>\n<\/tr>\n<tr>\n<td>[a-z]<\/td>\n<td>Any character, a-z (lowercase)<\/td>\n<\/tr>\n<tr>\n<td>[A-Za-z]<\/td>\n<td>Any character, a-z (case insensitive)<\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>One or more of the previous expression (e.g., \\d+ matches one or more digit)<\/td>\n<\/tr>\n<tr>\n<td>[^\/]+<\/td>\n<td>All characters except forward slash<\/td>\n<\/tr>\n<tr>\n<td>?<\/td>\n<td>Zero or more of the previous expression (e.g., \\d* matches zero or more digits)<\/td>\n<\/tr>\n<tr>\n<td>{1,3}<\/td>\n<td>Between one and three (inclusive) of the previous expression<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n, Home.as_view(), name='home')Including another URLconf&nbsp;&nbsp;&nbsp; \n\n1. Import the include() function: from django.conf.urls import url, include&nbsp;&nbsp;&nbsp;\n\n 2. Add a URL to urlpatterns:&nbsp; url(r'^blog\/', include('blog.urls'))\n\n\"\"\"from django.conf.urls import url\n\n&nbsp;&nbsp;&nbsp;&nbsp; from django.contrib import admin&nbsp;urlpatterns = [&nbsp;&nbsp;&nbsp;\n\n&nbsp;&nbsp;&nbsp;&nbsp; url(r'^admin\/', admin.site.urls),]\n\n\n<\/pre>\n<p>If we ignore the documentation comments at the top of the file, here\u2019s the essence of a URLconf:<\/p>\n<pre wp-pre-tag-2=\"\"><\/pre>\n<p>Let\u2019s step through this code one line at a time:<\/p>\n<ul>\n<li>The first line imports two functions from the django.conf.urls module: include which allows you to include a full Python import path to another URLconf module, and url which uses a regular expression to pattern match the URL in your browser to a module in your Django project.<\/li>\n<li>The second line calls the function admin from the django.contrib module. This function is called by the include function to load the URLs for the Django admin site.<\/li>\n<li>The third line is urlpatterns \u2013 a simple list of url() instances.<\/li>\n<\/ul>\n<p>The main thing to note here is the variable urlpatterns, which Django expects to find in your URLconf module. This variable defines the mapping between URLs and the code that handles those URLs. To add a URL and view to the URLconf, just add a mapping between a URL pattern and the view function. Here\u2019s how to hook in our hello view:<\/p>\n<pre wp-pre-tag-3=\"\"><\/pre>\n<p>We made two changes here:<\/p>\n<ul>\n<li>First, we imported the hello view from its module \u2013 mysite\/views.py, which translates into mysite.views in Python import syntax. (This assumes mysite\/views.py is on your Python path.)<\/li>\n<li>Next, we added the line url(r'^hello\/$', hello), to urlpatterns. This line is referred to as a URLpattern. The url() function tells Django how to handle the URL that you are configuring. The first argument is a pattern-matching string (a regular expression; more on this in a bit) and the second argument is the view function to use for that pattern. url() can take other optional arguments as well.<\/li>\n<\/ul>\n<p>One more important detail we\u2019ve introduced here is that \u2018r\u2018 character in front of the regular expression string. This tells Python that the string is a \u201craw string\u201d \u2013 its contents should not interpret backslashes. In normal Python strings, backslashes are used for escaping special characters \u2013 such as in the string \u201c\\n\u201c, which is a one-character string containing a newline.<\/p>\n<p>When you add the r to make it a raw string, Python does not apply its backslash escaping \u2013 so, \u201cr'\\n'\u201d is a two-character string containing a literal backslash and a lowercase \u201cn\u201d. There\u2019s a natural collision between Python\u2019s usage of backslashes and the backslashes that are found in regular expressions, so it\u2019s best practice to use raw strings any time you\u2019re defining a regular expression in Django.<\/p>\n<p>In a nutshell, we just told Django that any request to the URL \/hello\/ should be handled by the hello view function. It\u2019s worth discussing the syntax of this URLpattern, as it may not be immediately obvious. Although we want to match the URL \/hello\/, the pattern looks a bit different than that. Here\u2019s why:<\/p>\n<ul>\n<li>Django removes the slash from the front of every incoming URL before it checks the URLpatterns. This means that our URLpattern doesn\u2019t include the leading slash in \/hello\/. At first, this may seem unintuitive, but this requirement simplifies things \u2013 such as the inclusion of URLconfs within other URLconfs.<\/li>\n<li>The pattern includes a caret (^) and a dollar sign ($). These are regular expression characters that have a special meaning: the caret means \u201crequire that the pattern matches the start of the string,\u201d and the dollar sign means \u201crequire that the pattern matches the end of the string.\u201d<\/li>\n<\/ul>\n<p>This concept is best explained by example. If we had instead used the pattern ^hello\/(without a dollar sign at the end), then any URL starting with \/hello\/ would match, such as \/hello\/foo and \/hello\/bar, not just \/hello\/.<\/p>\n<p>Similarly, if we had left off the initial caret character (i.e., hello\/$), Django would match any URL that ends with hello\/, such as \/foo\/bar\/hello\/. If we had simply used hello\/, without a caret or dollar sign, then any URL containing hello\/ would match, such as \/foo\/hello\/bar.Thus, we use both the caret and dollar sign to ensure that only the URL \/hello\/ matches \u2013 nothing more, nothing less.<\/p>\n<p>Most of your URLpatterns will start with carets and end with dollar signs, but it\u2019s nice to have the flexibility to perform more sophisticated matches. You may be wondering what happens if someone requests the URL \/hello (that is, without a trailing slash). Because our URLpattern requires a trailing slash, that URL would not match. However, by default, any request to a URL that doesn\u2019t match a URLpattern and doesn\u2019t end with a slash will be redirected to the same URL with a trailing slash (This is regulated by the APPEND_SLASH Django setting, which is covered in Appendix D).<\/p>\n<p>The other thing to note about this URLconf is that we\u2019ve passed the hello view function as an object without calling the function. This is a key feature of Python (and other dynamic languages): functions are first-class objects, which means you can pass them around just like any other variables. Cool stuff, eh?<\/p>\n<p>To test our changes to the URLconf, start the Django development server, by running the command python manage.py runserver from within your Python virtual environment. (If you left it running, that\u2019s fine, too. The development server automatically detects changes to your Python code and reloads as necessary, so you don\u2019t have to restart the server between changes.)<\/p>\n<p>The server is running at the address http:\/\/127.0.0.1:8000\/, so open up a web browser and go to http:\/\/127.0.0.1:8000\/hello\/. You should see the text \u201cHello world\u201d \u2013 the output of your Django view, as below<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-128514 aligncenter\" src=\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/1-380x214.jpg\" alt=\"\" width=\"380\" height=\"214\"><\/p>\n<h3>Regular Expressions<\/h3>\n<p>Regular expressions (or regexes) are a compact way of specifying patterns in text. While Django URLconfs allow arbitrary regexes for powerful URL-matching capability, you\u2019ll probably use only a few regex patterns in practice. Here\u2019s a small selection of common patterns:<\/p>\n<p>&nbsp;<\/p>\n<table style=\"height: 667px;\" width=\"617\">\n<thead>\n<tr>\n<td><strong>Symbol<\/strong><\/td>\n<td><strong>Matches<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>. (dot)<\/td>\n<td>Any character<\/td>\n<\/tr>\n<tr>\n<td>\\d<\/td>\n<td>Any digit<\/td>\n<\/tr>\n<tr>\n<td>[A-Z]<\/td>\n<td>Any character, A-Z (uppercase)<\/td>\n<\/tr>\n<tr>\n<td>[a-z]<\/td>\n<td>Any character, a-z (lowercase)<\/td>\n<\/tr>\n<tr>\n<td>[A-Za-z]<\/td>\n<td>Any character, a-z (case insensitive)<\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>One or more of the previous expression (e.g., \\d+ matches one or more digit)<\/td>\n<\/tr>\n<tr>\n<td>[^\/]+<\/td>\n<td>All characters except forward slash<\/td>\n<\/tr>\n<tr>\n<td>?<\/td>\n<td>Zero or more of the previous expression (e.g., \\d* matches zero or more digits)<\/td>\n<\/tr>\n<tr>\n<td>{1,3}<\/td>\n<td>Between one and three (inclusive) of the previous expression<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n, hello),]\n\n\n<\/pre>\n<p>We made two changes here:<\/p>\n<ul>\n<li>First, we imported the hello view from its module \u2013 mysite\/views.py, which translates into mysite.views in Python import syntax. (This assumes mysite\/views.py is on your Python path.)<\/li>\n<li>Next, we added the line url(r&#8217;^hello\/$&#8217;, hello), to urlpatterns. This line is referred to as a URLpattern. The url() function tells Django how to handle the URL that you are configuring. The first argument is a pattern-matching string (a regular expression; more on this in a bit) and the second argument is the view function to use for that pattern. url() can take other optional arguments as well.<\/li>\n<\/ul>\n<p>One more important detail we\u2019ve introduced here is that \u2018r\u2018 character in front of the regular expression string. This tells Python that the string is a \u201craw string\u201d \u2013 its contents should not interpret backslashes. In normal Python strings, backslashes are used for escaping special characters \u2013 such as in the string \u201c\\n\u201c, which is a one-character string containing a newline.<\/p>\n<p>When you add the r to make it a raw string, Python does not apply its backslash escaping \u2013 so, \u201cr&#8217;\\n&#8217;\u201d is a two-character string containing a literal backslash and a lowercase \u201cn\u201d. There\u2019s a natural collision between Python\u2019s usage of backslashes and the backslashes that are found in regular expressions, so it\u2019s best practice to use raw strings any time you\u2019re defining a regular expression in Django.<\/p>\n<p>In a nutshell, we just told Django that any request to the URL \/hello\/ should be handled by the hello view function. It\u2019s worth discussing the syntax of this URLpattern, as it may not be immediately obvious. Although we want to match the URL \/hello\/, the pattern looks a bit different than that. Here\u2019s why:<\/p>\n<ul>\n<li>Django removes the slash from the front of every incoming URL before it checks the URLpatterns. This means that our URLpattern doesn\u2019t include the leading slash in \/hello\/. At first, this may seem unintuitive, but this requirement simplifies things \u2013 such as the inclusion of URLconfs within other URLconfs.<\/li>\n<li>The pattern includes a caret (^) and a dollar sign ($). These are regular expression characters that have a special meaning: the caret means \u201crequire that the pattern matches the start of the string,\u201d and the dollar sign means \u201crequire that the pattern matches the end of the string.\u201d<\/li>\n<\/ul>\n<p>This concept is best explained by example. If we had instead used the pattern ^hello\/(without a dollar sign at the end), then any URL starting with \/hello\/ would match, such as \/hello\/foo and \/hello\/bar, not just \/hello\/.<\/p>\n<p>Similarly, if we had left off the initial caret character (i.e., hello\/$), Django would match any URL that ends with hello\/, such as \/foo\/bar\/hello\/. If we had simply used hello\/, without a caret or dollar sign, then any URL containing hello\/ would match, such as \/foo\/hello\/bar.Thus, we use both the caret and dollar sign to ensure that only the URL \/hello\/ matches \u2013 nothing more, nothing less.<\/p>\n<p>Most of your URLpatterns will start with carets and end with dollar signs, but it\u2019s nice to have the flexibility to perform more sophisticated matches. You may be wondering what happens if someone requests the URL \/hello (that is, without a trailing slash). Because our URLpattern requires a trailing slash, that URL would not match. However, by default, any request to a URL that doesn\u2019t match a URLpattern and doesn\u2019t end with a slash will be redirected to the same URL with a trailing slash (This is regulated by the APPEND_SLASH Django setting, which is covered in Appendix D).<\/p>\n<p>The other thing to note about this URLconf is that we\u2019ve passed the hello view function as an object without calling the function. This is a key feature of Python (and other dynamic languages): functions are first-class objects, which means you can pass them around just like any other variables. Cool stuff, eh?<\/p>\n<p>To test our changes to the URLconf, start the Django development server, by running the command python manage.py runserver from within your Python virtual environment. (If you left it running, that\u2019s fine, too. The development server automatically detects changes to your Python code and reloads as necessary, so you don\u2019t have to restart the server between changes.)<\/p>\n<p>The server is running at the address http:\/\/127.0.0.1:8000\/, so open up a web browser and go to http:\/\/127.0.0.1:8000\/hello\/. You should see the text \u201cHello world\u201d \u2013 the output of your Django view, as below<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-128514 aligncenter\" src=\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/1-380x214.jpg\" alt=\"\" width=\"380\" height=\"214\"><\/p>\n<h3>Regular Expressions<\/h3>\n<p>Regular expressions (or regexes) are a compact way of specifying patterns in text. While Django URLconfs allow arbitrary regexes for powerful URL-matching capability, you\u2019ll probably use only a few regex patterns in practice. Here\u2019s a small selection of common patterns:<\/p>\n<p>&nbsp;<\/p>\n<table style=\"height: 667px;\" width=\"617\">\n<thead>\n<tr>\n<td><strong>Symbol<\/strong><\/td>\n<td><strong>Matches<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>. (dot)<\/td>\n<td>Any character<\/td>\n<\/tr>\n<tr>\n<td>\\d<\/td>\n<td>Any digit<\/td>\n<\/tr>\n<tr>\n<td>[A-Z]<\/td>\n<td>Any character, A-Z (uppercase)<\/td>\n<\/tr>\n<tr>\n<td>[a-z]<\/td>\n<td>Any character, a-z (lowercase)<\/td>\n<\/tr>\n<tr>\n<td>[A-Za-z]<\/td>\n<td>Any character, a-z (case insensitive)<\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>One or more of the previous expression (e.g., \\d+ matches one or more digit)<\/td>\n<\/tr>\n<tr>\n<td>[^\/]+<\/td>\n<td>All characters except forward slash<\/td>\n<\/tr>\n<tr>\n<td>?<\/td>\n<td>Zero or more of the previous expression (e.g., \\d* matches zero or more digits)<\/td>\n<\/tr>\n<tr>\n<td>{1,3}<\/td>\n<td>Between one and three (inclusive) of the previous expression<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n, views.home, name=&#8217;home&#8217;)Class-based views&nbsp;&nbsp;&nbsp;\n\n 1. Add an import:&nbsp; from other_app.views import Home&nbsp;&nbsp;&nbsp; \n\n2. Add a URL to urlpatterns:&nbsp; url(r&#8217;^\n<p>If we ignore the documentation comments at the top of the file, here\u2019s the essence of a URLconf:<\/p>\n<pre wp-pre-tag-2=\"\"><\/pre>\n<p>Let\u2019s step through this code one line at a time:<\/p>\n<ul>\n<li>The first line imports two functions from the django.conf.urls module: include which allows you to include a full Python import path to another URLconf module, and url which uses a regular expression to pattern match the URL in your browser to a module in your Django project.<\/li>\n<li>The second line calls the function admin from the django.contrib module. This function is called by the include function to load the URLs for the Django admin site.<\/li>\n<li>The third line is urlpatterns \u2013 a simple list of url() instances.<\/li>\n<\/ul>\n<p>The main thing to note here is the variable urlpatterns, which Django expects to find in your URLconf module. This variable defines the mapping between URLs and the code that handles those URLs. To add a URL and view to the URLconf, just add a mapping between a URL pattern and the view function. Here\u2019s how to hook in our hello view:<\/p>\n<pre wp-pre-tag-3=\"\"><\/pre>\n<p>We made two changes here:<\/p>\n<ul>\n<li>First, we imported the hello view from its module \u2013 mysite\/views.py, which translates into mysite.views in Python import syntax. (This assumes mysite\/views.py is on your Python path.)<\/li>\n<li>Next, we added the line url(r&#8217;^hello\/$&#8217;, hello), to urlpatterns. This line is referred to as a URLpattern. The url() function tells Django how to handle the URL that you are configuring. The first argument is a pattern-matching string (a regular expression; more on this in a bit) and the second argument is the view function to use for that pattern. url() can take other optional arguments as well.<\/li>\n<\/ul>\n<p>One more important detail we\u2019ve introduced here is that \u2018r\u2018 character in front of the regular expression string. This tells Python that the string is a \u201craw string\u201d \u2013 its contents should not interpret backslashes. In normal Python strings, backslashes are used for escaping special characters \u2013 such as in the string \u201c\\n\u201c, which is a one-character string containing a newline.<\/p>\n<p>When you add the r to make it a raw string, Python does not apply its backslash escaping \u2013 so, \u201cr&#8217;\\n&#8217;\u201d is a two-character string containing a literal backslash and a lowercase \u201cn\u201d. There\u2019s a natural collision between Python\u2019s usage of backslashes and the backslashes that are found in regular expressions, so it\u2019s best practice to use raw strings any time you\u2019re defining a regular expression in Django.<\/p>\n<p>In a nutshell, we just told Django that any request to the URL \/hello\/ should be handled by the hello view function. It\u2019s worth discussing the syntax of this URLpattern, as it may not be immediately obvious. Although we want to match the URL \/hello\/, the pattern looks a bit different than that. Here\u2019s why:<\/p>\n<ul>\n<li>Django removes the slash from the front of every incoming URL before it checks the URLpatterns. This means that our URLpattern doesn\u2019t include the leading slash in \/hello\/. At first, this may seem unintuitive, but this requirement simplifies things \u2013 such as the inclusion of URLconfs within other URLconfs.<\/li>\n<li>The pattern includes a caret (^) and a dollar sign ($). These are regular expression characters that have a special meaning: the caret means \u201crequire that the pattern matches the start of the string,\u201d and the dollar sign means \u201crequire that the pattern matches the end of the string.\u201d<\/li>\n<\/ul>\n<p>This concept is best explained by example. If we had instead used the pattern ^hello\/(without a dollar sign at the end), then any URL starting with \/hello\/ would match, such as \/hello\/foo and \/hello\/bar, not just \/hello\/.<\/p>\n<p>Similarly, if we had left off the initial caret character (i.e., hello\/$), Django would match any URL that ends with hello\/, such as \/foo\/bar\/hello\/. If we had simply used hello\/, without a caret or dollar sign, then any URL containing hello\/ would match, such as \/foo\/hello\/bar.Thus, we use both the caret and dollar sign to ensure that only the URL \/hello\/ matches \u2013 nothing more, nothing less.<\/p>\n<p>Most of your URLpatterns will start with carets and end with dollar signs, but it\u2019s nice to have the flexibility to perform more sophisticated matches. You may be wondering what happens if someone requests the URL \/hello (that is, without a trailing slash). Because our URLpattern requires a trailing slash, that URL would not match. However, by default, any request to a URL that doesn\u2019t match a URLpattern and doesn\u2019t end with a slash will be redirected to the same URL with a trailing slash (This is regulated by the APPEND_SLASH Django setting, which is covered in Appendix D).<\/p>\n<p>The other thing to note about this URLconf is that we\u2019ve passed the hello view function as an object without calling the function. This is a key feature of Python (and other dynamic languages): functions are first-class objects, which means you can pass them around just like any other variables. Cool stuff, eh?<\/p>\n<p>To test our changes to the URLconf, start the Django development server, by running the command python manage.py runserver from within your Python virtual environment. (If you left it running, that\u2019s fine, too. The development server automatically detects changes to your Python code and reloads as necessary, so you don\u2019t have to restart the server between changes.)<\/p>\n<p>The server is running at the address http:\/\/127.0.0.1:8000\/, so open up a web browser and go to http:\/\/127.0.0.1:8000\/hello\/. You should see the text \u201cHello world\u201d \u2013 the output of your Django view, as below<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-128514 aligncenter\" src=\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/1-380x214.jpg\" alt=\"\" width=\"380\" height=\"214\"><\/p>\n<h3>Regular Expressions<\/h3>\n<p>Regular expressions (or regexes) are a compact way of specifying patterns in text. While Django URLconfs allow arbitrary regexes for powerful URL-matching capability, you\u2019ll probably use only a few regex patterns in practice. Here\u2019s a small selection of common patterns:<\/p>\n<p>&nbsp;<\/p>\n<table style=\"height: 667px;\" width=\"617\">\n<thead>\n<tr>\n<td><strong>Symbol<\/strong><\/td>\n<td><strong>Matches<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>. (dot)<\/td>\n<td>Any character<\/td>\n<\/tr>\n<tr>\n<td>\\d<\/td>\n<td>Any digit<\/td>\n<\/tr>\n<tr>\n<td>[A-Z]<\/td>\n<td>Any character, A-Z (uppercase)<\/td>\n<\/tr>\n<tr>\n<td>[a-z]<\/td>\n<td>Any character, a-z (lowercase)<\/td>\n<\/tr>\n<tr>\n<td>[A-Za-z]<\/td>\n<td>Any character, a-z (case insensitive)<\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>One or more of the previous expression (e.g., \\d+ matches one or more digit)<\/td>\n<\/tr>\n<tr>\n<td>[^\/]+<\/td>\n<td>All characters except forward slash<\/td>\n<\/tr>\n<tr>\n<td>?<\/td>\n<td>Zero or more of the previous expression (e.g., \\d* matches zero or more digits)<\/td>\n<\/tr>\n<tr>\n<td>{1,3}<\/td>\n<td>Between one and three (inclusive) of the previous expression<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n, Home.as_view(), name=&#8217;home&#8217;)Including another URLconf&nbsp;&nbsp;&nbsp; \n\n1. Import the include() function: from django.conf.urls import url, include&nbsp;&nbsp;&nbsp;\n\n 2. Add a URL to urlpatterns:&nbsp; url(r&#8217;^blog\/&#8217;, include(&#8216;blog.urls&#8217;))\n\n&#8220;&#8221;&#8221;from django.conf.urls import url\n\n&nbsp;&nbsp;&nbsp;&nbsp; from django.contrib import admin&nbsp;urlpatterns = [&nbsp;&nbsp;&nbsp;\n\n&nbsp;&nbsp;&nbsp;&nbsp; url(r&#8217;^admin\/&#8217;, admin.site.urls),]\n\n\n\n<p>If we ignore the documentation comments at the top of the file, here\u2019s the essence of a URLconf:<\/p>\n<pre wp-pre-tag-2=\"\"><\/pre>\n<p>Let\u2019s step through this code one line at a time:<\/p>\n<ul>\n<li>The first line imports two functions from the django.conf.urls module: include which allows you to include a full Python import path to another URLconf module, and url which uses a regular expression to pattern match the URL in your browser to a module in your Django project.<\/li>\n<li>The second line calls the function admin from the django.contrib module. This function is called by the include function to load the URLs for the Django admin site.<\/li>\n<li>The third line is urlpatterns \u2013 a simple list of url() instances.<\/li>\n<\/ul>\n<p>The main thing to note here is the variable urlpatterns, which Django expects to find in your URLconf module. This variable defines the mapping between URLs and the code that handles those URLs. To add a URL and view to the URLconf, just add a mapping between a URL pattern and the view function. Here\u2019s how to hook in our hello view:<\/p>\n<pre wp-pre-tag-3=\"\"><\/pre>\n<p>We made two changes here:<\/p>\n<ul>\n<li>First, we imported the hello view from its module \u2013 mysite\/views.py, which translates into mysite.views in Python import syntax. (This assumes mysite\/views.py is on your Python path.)<\/li>\n<li>Next, we added the line url(r&#8217;^hello\/$&#8217;, hello), to urlpatterns. This line is referred to as a URLpattern. The url() function tells Django how to handle the URL that you are configuring. The first argument is a pattern-matching string (a regular expression; more on this in a bit) and the second argument is the view function to use for that pattern. url() can take other optional arguments as well.<\/li>\n<\/ul>\n<p>One more important detail we\u2019ve introduced here is that \u2018r\u2018 character in front of the regular expression string. This tells Python that the string is a \u201craw string\u201d \u2013 its contents should not interpret backslashes. In normal Python strings, backslashes are used for escaping special characters \u2013 such as in the string \u201c\\n\u201c, which is a one-character string containing a newline.<\/p>\n<p>When you add the r to make it a raw string, Python does not apply its backslash escaping \u2013 so, \u201cr&#8217;\\n&#8217;\u201d is a two-character string containing a literal backslash and a lowercase \u201cn\u201d. There\u2019s a natural collision between Python\u2019s usage of backslashes and the backslashes that are found in regular expressions, so it\u2019s best practice to use raw strings any time you\u2019re defining a regular expression in Django.<\/p>\n<p>In a nutshell, we just told Django that any request to the URL \/hello\/ should be handled by the hello view function. It\u2019s worth discussing the syntax of this URLpattern, as it may not be immediately obvious. Although we want to match the URL \/hello\/, the pattern looks a bit different than that. Here\u2019s why:<\/p>\n<ul>\n<li>Django removes the slash from the front of every incoming URL before it checks the URLpatterns. This means that our URLpattern doesn\u2019t include the leading slash in \/hello\/. At first, this may seem unintuitive, but this requirement simplifies things \u2013 such as the inclusion of URLconfs within other URLconfs.<\/li>\n<li>The pattern includes a caret (^) and a dollar sign ($). These are regular expression characters that have a special meaning: the caret means \u201crequire that the pattern matches the start of the string,\u201d and the dollar sign means \u201crequire that the pattern matches the end of the string.\u201d<\/li>\n<\/ul>\n<p>This concept is best explained by example. If we had instead used the pattern ^hello\/(without a dollar sign at the end), then any URL starting with \/hello\/ would match, such as \/hello\/foo and \/hello\/bar, not just \/hello\/.<\/p>\n<p>Similarly, if we had left off the initial caret character (i.e., hello\/$), Django would match any URL that ends with hello\/, such as \/foo\/bar\/hello\/. If we had simply used hello\/, without a caret or dollar sign, then any URL containing hello\/ would match, such as \/foo\/hello\/bar.Thus, we use both the caret and dollar sign to ensure that only the URL \/hello\/ matches \u2013 nothing more, nothing less.<\/p>\n<p>Most of your URLpatterns will start with carets and end with dollar signs, but it\u2019s nice to have the flexibility to perform more sophisticated matches. You may be wondering what happens if someone requests the URL \/hello (that is, without a trailing slash). Because our URLpattern requires a trailing slash, that URL would not match. However, by default, any request to a URL that doesn\u2019t match a URLpattern and doesn\u2019t end with a slash will be redirected to the same URL with a trailing slash (This is regulated by the APPEND_SLASH Django setting, which is covered in Appendix D).<\/p>\n<p>The other thing to note about this URLconf is that we\u2019ve passed the hello view function as an object without calling the function. This is a key feature of Python (and other dynamic languages): functions are first-class objects, which means you can pass them around just like any other variables. Cool stuff, eh?<\/p>\n<p>To test our changes to the URLconf, start the Django development server, by running the command python manage.py runserver from within your Python virtual environment. (If you left it running, that\u2019s fine, too. The development server automatically detects changes to your Python code and reloads as necessary, so you don\u2019t have to restart the server between changes.)<\/p>\n<p>The server is running at the address http:\/\/127.0.0.1:8000\/, so open up a web browser and go to http:\/\/127.0.0.1:8000\/hello\/. You should see the text \u201cHello world\u201d \u2013 the output of your Django view, as below<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-128514 aligncenter\" src=\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/1-380x214.jpg\" alt=\"\" width=\"380\" height=\"214\"><\/p>\n<h3>Regular Expressions<\/h3>\n<p>Regular expressions (or regexes) are a compact way of specifying patterns in text. While Django URLconfs allow arbitrary regexes for powerful URL-matching capability, you\u2019ll probably use only a few regex patterns in practice. Here\u2019s a small selection of common patterns:<\/p>\n<p>&nbsp;<\/p>\n<table style=\"height: 667px;\" width=\"617\">\n<thead>\n<tr>\n<td><strong>Symbol<\/strong><\/td>\n<td><strong>Matches<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>. (dot)<\/td>\n<td>Any character<\/td>\n<\/tr>\n<tr>\n<td>\\d<\/td>\n<td>Any digit<\/td>\n<\/tr>\n<tr>\n<td>[A-Z]<\/td>\n<td>Any character, A-Z (uppercase)<\/td>\n<\/tr>\n<tr>\n<td>[a-z]<\/td>\n<td>Any character, a-z (lowercase)<\/td>\n<\/tr>\n<tr>\n<td>[A-Za-z]<\/td>\n<td>Any character, a-z (case insensitive)<\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>One or more of the previous expression (e.g., \\d+ matches one or more digit)<\/td>\n<\/tr>\n<tr>\n<td>[^\/]+<\/td>\n<td>All characters except forward slash<\/td>\n<\/tr>\n<tr>\n<td>?<\/td>\n<td>Zero or more of the previous expression (e.g., \\d* matches zero or more digits)<\/td>\n<\/tr>\n<tr>\n<td>{1,3}<\/td>\n<td>Between one and three (inclusive) of the previous expression<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n<p><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/certified-django-developer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Back to Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, to recap, this view function returns an HTML page that includes the current date and time. But how do we tell Django to use this code? That\u2019s where URLconfs come in. If, at this point, you ran python manage.py runserver again, you\u2019d still see the \u201cWelcome to Django\u201d message, with no trace of our&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8655],"tags":[8658],"class_list":["post-75182","page","type-page","status-publish","hentry","category-django-web-development","tag-mapping-urls-to-views"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Mapping URLs to Views - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Mapping URLs to Views - Tutorial\" \/>\n<meta property=\"og:description\" content=\"So, to recap, this view function returns an HTML page that includes the current date and time. But how do we tell Django to use this code? That\u2019s where URLconfs come in. If, at this point, you ran python manage.py runserver again, you\u2019d still see the \u201cWelcome to Django\u201d message, with no trace of our...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:47:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/1-380x214.jpg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"33 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/\",\"name\":\"Mapping URLs to Views - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/1-380x214.jpg\",\"datePublished\":\"2020-01-18T06:52:50+00:00\",\"dateModified\":\"2024-04-12T08:47:11+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/#primaryimage\",\"url\":\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/1-380x214.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/1-380x214.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Mapping URLs to Views\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Mapping URLs to Views - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/","og_locale":"en_US","og_type":"article","og_title":"Mapping URLs to Views - Tutorial","og_description":"So, to recap, this view function returns an HTML page that includes the current date and time. But how do we tell Django to use this code? That\u2019s where URLconfs come in. If, at this point, you ran python manage.py runserver again, you\u2019d still see the \u201cWelcome to Django\u201d message, with no trace of our...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:47:11+00:00","og_image":[{"url":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/1-380x214.jpg","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"33 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/","name":"Mapping URLs to Views - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/#primaryimage"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/#primaryimage"},"thumbnailUrl":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/1-380x214.jpg","datePublished":"2020-01-18T06:52:50+00:00","dateModified":"2024-04-12T08:47:11+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/#primaryimage","url":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/1-380x214.jpg","contentUrl":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/1-380x214.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/mapping-urls-to-views\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Mapping URLs to Views"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=75182"}],"version-history":[{"count":4,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75182\/revisions"}],"predecessor-version":[{"id":83319,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75182\/revisions\/83319"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=75182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=75182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=75182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}