We use cookies to create a better browsing experience. By using this website you consent to our cookies policy.

Accept

DJANGO CUSTOM WEBPAGE

By ADMIN| October 29, 2018

CATEGORY : BLOG | BLOG DYNAMIC | BUSINESS TRAVEL | DEV | LATEST NEWS

TAGS :

Django is a free and open-source high level Python web framework. It follows the model-view-template architectural pattern. Today in this article, we’re going to create our first custom webpage in django. This is for you to understand how to add a custom URL to any page, how to add custom webpage in the website, create multiple web-pages in django and flow of information in the website. For example, if someone asks for specific url then how do we route them into the correct places and ultimately give them back some HTML.Before reading the tutorial start your local server using python3 manage.py runserver command in project directory. Whenever we create a project and run it in browser then django’s default page shows up.That’s not exactly what we want. So let’s see how to create our own webpage in django. We know that if someone is looking for a URL on the website it comes to this “urls.py”.Right now we’ve path of admin/ in the list url patterns. That means when user goes to the website (http://127.0.0.1:8000/) and add a /admin in the url, user will be redirected to admin page of the django website.

How to Change URL of Existing Web pages?

  • If we want to change the URL for our admin page, then we can modify the urls.py file like this:
  • Open your web browser and enter the domain-name/admin (http://127.0.0.1:8000/admin). The result will show an error that page cannot be found because we have changed the address of our admin page. So our new address is domain-name/mypage (http://127.0.0.1:8000/mypage). Open this address and the result will be:
  • That how we can change any existing web-page’s URL. Remember, here if you need to make a change in a file, it is going to auto-reload the server for you. So we don’t have to manually go back to stop and start the server to save the new changes.

Create Your Own Custom Web page in Django

  • Firstly, we don’t need that admin page so you can delete that path entirely.
In fact, we can delete the first line about admin in the urls.py as we do not require the admin.
  • Now let us assume that we’re going to add a path for our own page. May be the homepage. So when someone comes to the homepage of the website then we’ll show them the custom homepage instead of that default django template page. To do this, open the urls.py and add a new path. Basically if someone comes to the homepage means they don’t need anything extra except the domain-name (http://127.0.0.1:8000/), so we’ll put an empty string in path, like this:
  • Put a comma after the empty string and after comma (,) we’ll add something which will help to redirect where we want to send when someone comes to our homepage.
  • Here we have to create a new file called views.py which essentially allows us to send back some information. So we’ll create a new file in same directory where urls.py exists.
  • To use views.py into the urls.py, we’ve to import views.py in urls.py file. So open urls.py and add the following line:
  • Here dot (.) means current directory. Now add a function to call in our path into urls.py.
  • It is showing that if someone goes to our homepage then the function ‘home’ is called which is located in the views.py, but we don’t have any function called home in the views.py yet.
  • Open views.py and add a new function called home.
  • Here we have to pass the request parameter in home function. Whenever someone is coming to the URL of our website, it sends this request object. It means that the url they are looking for and more advanced information like cookies and what browser they are using. So that type of information comes through this request object.
  • We return something back to the user using return keyword. But we can’t return a simple string back from our function, we’ve to give back an HTTP response. So in order to do that we’ve to use function HttpResponse(string) and to use HttpsResponse(string) we’ve to import some package usingfrom django.http import HttpResponse. Now save this file and reload your website. The output will be:

Flow of Information in Django Website

If someone tries to open the website’s homepage (http://127.0.0.1:8000/) then they will be redirected to the urls.py file. Urls.py check the entered url by the user. It will check for the empty string. As there is one path having an empty string, it will be redirected to the function written in path with empty string, that is views.home in our case. The home function in views.py is returning HttpsResponse that is ‘hello’. So user have the information that he/she requested for. That’s how the flow of information works in django.

Creating Multiple Web pages in Django

As we make one custom webpage, we can also add some more web-pages having unique addresses assigned to them.
  • Open urls.py and add a new path like:
  • Open views.py and create a new function for page1.
  • That’s how we can create multiple pages in our website. The string we’re returning is HTML so we can also use HTML tags in it like:
and refresh http://127.0.0.1:8000/page1 

Have a
question?

Our team is here to help

CONTACT US

OPERATIONS OFFICE:

2F-CS, 46 Ansal Plaza, Sector-1
Vaishali, Ghaziabad-201010 (UP), India

CORPORATE OFFICE:

1002,1004 Antriksh Bhawan
22, KG Marg, New Delhi – 110001

MESSAGE US

info@kreatetechnologies.com

+91 11 66666999