How to Create Django Views?
In this guide, you will learn step by step how to create View functions in Django, bind URL configurations, and pull dynamic data from the database and display this data on the web page. You can make the contents in your web application dynamic with Views, one of the core components of Django.
1. Defining View Functions
View functions receive the web request and return the corresponding HTTP response (usually an HTML page). Django uses this function to retrieve model data and present it to the user with templates.
1.1 Virtual Environment and Directory Preparation
Prepare your working environment by activating your virtual environment and navigating to the application directory.
cd ~/my_blog_app
. env/bin/activate
cd blog/genixnode_blog
1.2 Editing the views.py File
By opening the views.py file containing the view functions, we will define two basic functions that return simple text responses.
nano views.py
Add the following code:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse('Merhaba, anasayfaya hoş geldiniz.')
def individual_post(request):
return HttpResponse('Burası bireysel yazıların gösterileceği yerdir.')
These functions return simple HTTP responses for the homepage and posts.
2. Mapping URL Configuration (URLconf)
Now, let's connect these functions we created to specific URL paths. In Django, URL configurations are made (urls.py dosyasında).
2.1 Editing the Application URLconf File
Open the application urls.py and add the URL patterns as below.
nano urls.py
Code:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'), # Ana sayfa
path('post/', views.individual_post, name='individual_post') # Blog yazıları
]
This code creates the URLs / for the homepage and /post/ for the posts.
2.2 Updating the Main Project URLconf File
Include the application's URL configuration (URLconf) by opening the project's main urls.py file.
cd ~/my_blog_app/blog/blog
nano urls.py
Code:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('genixnode_blog.urls')) # Uygulama URL'lerini dahil et
]
This code includes the genixnode_blog app's URL configuration into the main project.
3. Dynamic Data Capture and Display
Let's extract the data from the blog posts we added via the admin panel and display it dynamically on the web page.
3.1 Adding a Record from the Admin Panel
First, add a new blog post via the admin panel. Run the server and log in to the admin panel:
http://your-server-ip:8000/admin/genixnode_blog/post/add/
Add a new post and save.
3.2 Updating views.py File
Now we will update the views.py file to pull dynamic data from the database.
cd ~/my_blog_app/blog/genixnode_blog
nano views.py
Code:
from django.shortcuts import render
from django.http import HttpResponse
from .models import Post
def index(request):
return HttpResponse('Merhaba, anasayfaya hoş geldiniz.')
def individual_post(request):
recent_post = Post.objects.get(id__exact=1) # ID'si 1 olan yazıyı çek
return HttpResponse(f'{recent_post.title}: {recent_post.content}') # Yazı başlığı ve içeriğini göster
This code pulls the text from the database from the Post model and displays this text on the web page.
3.3 Starting the Server
Start the server and go to http://your-server-ip:8000/post/ in the browser. The title and content of the article retrieved from the database will appear here.
cd ~/my_blog_app/blog
python manage.py runserver 0.0.0.0:8000
4. Frequently Asked Questions (FAQ)
- What are View functions in Django?
View functions are Python functions that respond to web requests and return appropriate HTTP responses. It often interacts with database transactions and templates.
- What is URLconf?
URLconf defines URL redirects in Django. That is, it determines which URL will be mapped to which view function.
- When is it more appropriate to use render instead of HttpResponse?
While HttpResponse returns plain text, the render function is used to return dynamic content with HTML templates and data.
- Is
Post.objects.get(id__exact=1)a correct method?
Yes, but this method is only suitable for capturing text with ID 1. In live applications, data is usually retrieved dynamically via URL parameters.
- How to do URL redirection in Django?
URLs are redirected with the path() function in the urls.py file. An appropriate view function is defined for each URL.
5. Conclusion
In this guide, you learned how to create basic views in Django, configure URLs, and pull dynamic data from the database. Now you can easily perform content management and dynamic page display operations in your Django application. You can manage the project you developed more efficiently by publishing it on the GenixNode virtual server.

