Skip to main content

Django Routing and Views

Django Routing and Views

In this article we are going to explore how django handles and render different html page at different urls

First we need to create a project and start a app which is already discussed in previous article.

In this article project name is demo and app name is demoApp like below



Now open project directory in your text editor, it looks like below.



Now, views.py is the place where we create our views for users. Let define our function that return a page index.html

def index(request):
return render(request,"demoApp/index.html")

Now we need to create index.html. In django every html file is called template and by default django search template file in templates folder of app, so create a folder templates inside app.

Now look at what the index function return, render index.html which is in the demoApp directory.

So inside templates directory create demoApp folder and inside this folder we write our index.html.

Look at this image carefully and observe where index.html is present.



Now we create a view but one thing missing routing, how to user route to this page, lets create it.

Notice in the above image, we create a urls.py inside demoApp that handle at what url , what function is execute in view.py that render a page.

Inside newly created urls.py of demoApp write the following code

from django.urls import path
from . import views # importing the view

urlpatterns = [

path(
"", views.index),

]

urlpatterns is the dictionary of path function that take care of at what url, what function is execute.

In the above example when the path is blank, run the index function of views (views.index).

Now we have two urls.py, one for project and one for demoApp, by default django looks project urls.py in start and then route to different app, we need to integrate our demoApp urls.py.

Some code are already written inside project urls.py (means demo.urls ), we need to add a path in urlpatterns which include our app urls.py at a specific path

from django.contrib import admin
from django.urls import path, include # import include urlpatterns = [
path('admin/', admin.site.urls),
path("", include('demoApp.urls')), # when path is blank include demoApp.urls ]

Now our app is almost ready, we create a view, route to view, template to render but one last thing is still missing.

We didn't tell the project demo that we have an app demoApp.

Inside demo/settings there is a dictionary of INSTALLED_APPS, just add demoApp inside a dictionary like this

INSTALLED_APPS = [

'demoApp',

.....

.....

.....

]

and we are done now go to the project directory and run server and open the URL in a web browser if the page looks like below image then you successfully create and render a page in django.


If you have any difficulties in the above process then let me know in the comment section.





Popular posts from this blog

Introduction to Java Security

Introduction to Java Security The Java security architecture includes a large set of application programming interfaces (APIs), tools, and implementations of commonly-used security algorithms, mechanisms, and protocols. The Java security APIs span a wide range of areas. Cryptographic and public key infrastructure (PKI) interfaces provide the underlying basis for developing secure applications. Interfaces for performing authentication and access control enable applications to guard against unauthorized access to protected resources. The JDK includes a number of providers that implement a core set of security services. It also allows for additional custom providers to be installed. This enables developers to extend the platform with new security mechanisms. The JDK is divided into modules. Modules that contain security APIs include the following:

Module Description java.base Defines the foundational APIs of Java SE;  contained packages include java.securityjavax.cryptojavax.net.ssl,  and…

SQL Injection

Overview A SQL injection attack consists of insertion or "injection" of a SQL query via the input data from the client to the application. A successful SQL injection exploit can read sensitive data from the database, modify database data (Insert/Update/Delete), execute administration operations on the database (such as shutdown the DBMS), recover the content of a given file present on the DBMS file system and in some cases issue commands to the operating system. SQL injection attacks are a type of injection attack, in which SQL commands are injected into data-plane input in order to effect the execution of predefined SQL commands. Threat ModelingSQL injection attacks allow attackers to spoof identity, tamper with existing data, cause repudiation issues such as voiding transactions or changing balances, allow the complete disclosure of all data on the system, destroy the data or make it otherwise unavailable, and become administrators of the database server.SQL Injection is ve…

Insertion Node in the Linkelist.

In this post, methods to insert a new node in linked list are discussed. A node can be added in three ways
1) At the front of the linked list
2) After a given node.
3) At the end of the linked list
public class Linkedlist { Node head; class Node{ int data; Node next; Node(int d){ data =d; next=null; } } // INSERT THE NODE AT THE BEGIN OF LINKEDLIST. public void insertAtfront(int new_data){ // Node temp = head; Node new_node = new Node(new_data); new_node.next = head; head = new_node; }  // INSERT THE NODE AT THE GIVEN POSITION IN LINKEDLIST.
public void insertAtGiven(Node prev_node,int new_data) { if(prev_node == null){ System.out.print("previous node can't be null"); return; } Node new_node = new Node(new_data); new_node.next = prev_node.next; prev_node.next = new_node; } // INSERT THE NODE  AT THE END OF THE LINKEDLIST.   public void insertAtEnd(int new_data){ Node new_node = new Node(new_data); new_node.nex…