Quick Contact

    Python Tutorial
    Python Panda Tutorial
    Python Selenium
    Python Flask Tutorial
    Python Django
    Numpy
    Tensorflow
    Interview Questions & Answers

    Django URLs and URLConf

    What is a URL?

    URL stands for Uniform Resource Locator. It is the address used by your server to search for the right webpage.

    All the servers take the URL which you searched in the browser and via that server, provides you the correct result and if they don’t find anything matching the URL, it will show 404 FILE NOT FOUND ERROR.

    URLs in Django

    How does Django Server interpret URLs?

    Django interprets URLs in a rather different way, the URLs in Django are in the format of regular expressions, which are easily readable by humans than the traditional URLs of PHP frameworks.

    Regular Expressions

    A Regular Expression also called RegEx is a format for search pattern which is much cleaner and easy to read for the humans and is very logical.

    That’s important because it makes the process of SEO much easier then it would be with the traditional URL approach which contains much more special characters.

    We will understand the regular expression along the way in the tutorial.

    How does the URL get searched and the file is delivered?

    When we search for a URL in the URL Bar like this:

    https://tutorials.ducatindia.com/python/django-models/

    The same URL is passed to the server after DNS connects us to the appropriate server. While your Django-server is running, it will receive the URL and search for the URL in the urls.py file or the value in the ROOT_URLCONF variable.

    Modifying urls.py file

    The urls.py file in Django is like the address book of your Django website. It stores all the web addresses for your website. It connects that to some view component or any other urls-conf file for a certain application.

    Importing the view function

    We will be learning the steps to import the view functions and connecting different url-conf files with the main urls.py file.

    AURLconf is similar to a table of contents for our Django-powered web site. It’s a mapping between URL patterns and the view functions that need to be called for those URLs.

    First, we will have to import the view function that we want our server to run when the URL is matched. For that, we have to import the function from the views.py file.

    Then just type in this code in views.py file:

    fromdjango.http import HttpResponse

    def index(request):

    html = “””< h1>Data Flair Django< /h1>Hello, you just configured you First URL”””

    returnHttpResponse(html)

    For that, we will first import the index function from our view file in the urls.py. Then, in the next step, we can add another element in the list with some parameters.

    Code:

    from .views import index

    This will import the index function from the views.py file.

    Then inside the urlpatterns list, add this item as:

    path(“dataflair/”, index)

    This function has different parameters for different cases since here we are passing a view function directly as we have imported the same. The first argument here is the URL you are going to search and it can be anything you want, I have kept it like this.

    At times, you will need to import the view file itself rather than the function. We will learn that process in upcoming tutorials.

     

     

     

    Apply now for Advanced Python Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.