Introduction to Django Views and how to create them(Part VIII)

In this tutorial, we are going to have a look at what Django views and how to create one for your local Blog Web Application.

This tutorial is a part of our series onĀ Creating Blog Website using Django.

Link to the Previous tutorial:

What are Views in Django

Views are basically functions that determine how the information will be displayed on the website. Just like functions, it takes information from Models and then transfers it to Templates to make it visible accordingly on the website.

It takes input in the form of web request and gives out output in form of web response.

In Django, the Views are placed/created in the views.py file.

Creating a View in Django

On your computer open up the file blog1/views.py in your code editor and add to it :

from django.shortcuts import render 
def post(request): 
    return render(request, 'blog/post.html', {})

This will create a View Post that put together the contents of the Template blog/post.html.

Now, go the command line and run the following command :

python manage.py runserver

 

But still, now we are getting an error; something like TemplateDoesNotExist/.

Django Template does not exist

Django: Template does not exist

This is because we haven’t yet created the template required to be rendered.

Don’t worry. That’s something we are going to do in the next tutorial.

Next Part of this Django Tutorial Series:

For now, if you have any doubt drop it in the comment section below.

Also, have a look at some other posts :

One response to “Introduction to Django Views and how to create them(Part VIII)”

  1. Cameron Helmuth says:

    This is the error I am getting

    Traceback (most recent call last):
    File “manage.py”, line 21, in
    main()
    File “manage.py”, line 17, in main
    execute_from_command_line(sys.argv)
    File “/home/cameron/software/PythonWebApps/blogsite/venv/lib/python3.6/site-packages/django/core/management/__init__.py”, line 371, in execute_from_command_line
    utility.execute()
    File “/home/cameron/software/PythonWebApps/blogsite/venv/lib/python3.6/site-packages/django/core/management/__init__.py”, line 317, in execute
    settings.INSTALLED_APPS
    File “/home/cameron/software/PythonWebApps/blogsite/venv/lib/python3.6/site-packages/django/conf/__init__.py”, line 56, in __getattr__
    self._setup(name)
    File “/home/cameron/softwTraceback (most recent call last):
    File “manage.py”, line 21, in
    main()
    File “manage.py”, line 17, in main
    execute_from_command_line(sys.argv)
    File “/home/cameron/software/PythonWebApps/blogsite/venv/lib/python3.6/site-packages/django/core/management/__init__.py”, line 371, in execute_from_command_line
    utility.execute()
    File “/home/cameron/software/PythonWebApps/blogsite/venv/lib/python3.6/site-packages/django/core/management/__init__.py”, line 317, in execute
    settings.INSTALLED_APPS
    File “/home/cameron/software/PythonWebApps/blogsite/venv/lib/python3.6/site-packages/django/conf/__init__.py”, line 56, in __getattr__
    self._setup(name)
    File “/home/cameron/software/PythonWebApps/blogsite/venv/lib/python3.6/site-packages/django/conf/__init__.py”, line 43, in _setup
    self._wrapped = Settings(settings_module)
    File “/home/cameron/software/PythonWebApps/blogsite/venv/lib/python3.6/site-packages/django/conf/__init__.py”, line 106, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
    File “/usr/lib/python3.6/importlib/__init__.py”, line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    File “”, line 994, in _gcd_import
    File “”, line 971, in _find_and_load
    File “”, line 941, in _find_and_load_unlocked
    File “”, line 219, in _call_with_frames_removed
    File “”, line 994, in _gcd_import
    File “”, line 971, in _find_and_load
    File “”, line 953, in _find_and_load_unlocked
    ModuleNotFoundError: No module named ‘blogare/PythonWebApps/blogsite/venv/lib/python3.6/site-packages/django/conf/__init__.py”, line 43, in _setup
    self._wrapped = Settings(settings_module)
    File “/home/cameron/software/PythonWebApps/blogsite/venv/lib/python3.6/site-packages/django/conf/__init__.py”, line 106, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
    File “/usr/lib/python3.6/importlib/__init__.py”, line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    File “”, line 994, in _gcd_import
    File “”, line 971, in _find_and_load
    File “”, line 941, in _find_and_load_unlocked
    File “”, line 219, in _call_with_frames_removed
    File “”, line 994, in _gcd_import
    File “”, line 971, in _find_and_load
    File “”, line 953, in _find_and_load_unlocked
    ModuleNotFoundError: No module named ‘blog

Leave a Reply

Your email address will not be published. Required fields are marked *