Create View in Django
In django, a view function is basically used for taking requests from the web and sending responses to the web. The response can be of different types like a html page or just one line httpResponse,etc. To use the view function, we have to use it with a route which will be responsible for calling that particular view.
So let us create a Simple View now.
Open the urls.py in the project folder and add the following code:
Here we added a new path in the urlpatterns list with the url as 'funda/'. Now run your project with command "python manage.py runserver" and type the url 'funda/' in the search bar like "http://localhost:8000/funda/" and django will go to the firstview() as given in the path and the HttResponse will be shown on the browser as "My first View in django".
We have successfully got the HttpResponse as the output in the browser. Now we dont want just a single line of response instead we want to return a full .html file as the output. To return the .html as the output, there are a few things that have to be done.
Step 1: Create a file with the name "urls.py" in the app folder and import the files as shown below:
Step 2: Link this file in the main project's urls.py file as shown below:
The path('', include('accounts.urls')) line tells django to search for the urls in our app folder's urls.py file(In our example, the appname is accounts) .So now as have seen in Step 1,we have given the path('funda/', views.index), so this means when we search for the url "funda/", it will go to the views.py file and search for a function called "index()".
Step 3: Open the views.py file in the app folder and create a function with name index() as shown below:
Inside the application folder, create a folder with the name "templates".Make sure you write the exact spelling because django will by default search for the templates folder inside the app.
The most important and confusing part: Inside the templates folder create a folder with the same name as of your app.The file structure will look as shown below:
This looks redundant but django will look for the appname inside the templates folder.
Create your Index.html file and write some html code in it as shown below:
Now run your project and search for the path given in the urls.py.(http://localhost:8000/funda/) and you will see the html output as shown below:
Tags: Funda of web it Django tutorials, Django tutorials, Django tutorials funda, django for beginners, how to create view in django, what is view in django, templates in django, templating in django
Latest PostsREST API using Django Rest Framework How to connect Django project with MySQL database How to delete image from folder when deleting a record in django How to update new image in django How to fetch / retrieve images in django How to upload image in django Deploy Django App on Heroku for free Upload Files/Images in Django How to show success message in django Delete data from database in Django
© Copyright - 2018 - 2022 | All rights reserved at Funda of Web IT