How to Solve Error Message render() missing 1 required positional argument: ‘template_name’ in Django Application

Posted on

Introduction

The error message appear in the title of this article is the main reason for this article. It is to show and describe how the error appear and how to solve it. This article has a connection with the previous article. That article has the title of ‘How to Solve Error Message : AttributeError: ‘str’ object has no attribute ‘get’ in Django’  and it exist in this link. Actually, the following is the actual display of the error message :

How to Solve Error Message render() missing 1 required positional argument: 'template_name' in Django Application
How to Solve Error Message render() missing 1 required positional argument: ‘template_name’ in Django Application

The following is the complete error message appear in the command line interface upon the Django-based application execution :

Internal Server Error: /sysapp
Traceback (most recent call last):
File "C:\programming\python\env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\programming\python\env\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\programming\python\myproject\sysapp\views.py", line 8, in main
render("templates/sysapp/main.html")
TypeError: render() missing 1 required positional argument: 'template_name'

So, as a journey to solve the problem for displaying only a static HTML file, there are several various attempt to do it.

Django-based Application Condition and Setting

Before getting on to the attempt, the following is the actual condition of the Django-based application :

  1. So, there is a Django-based application exist. Create one as in the following link for reference. It is an article to create a Django-based application with the title of ‘How to Create a New Application using Django Web-based Framework in Ubuntu Linux via Command Line’. Although it is in Ubuntu Linux, it also apply in any other operating system.

  2. Add and register the application as an ‘installed’ application in the ‘settings.py’ file of the project. In this context, the application name is ‘sysapp’. It exist as in the following content :

    # Application definition
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'crispy_forms',
        'sysapp',
    ]
  3. Next step is creating necessary files for the application. First of all, the ‘urls.py’ for defining the URL of the application. The content exist as follows :

    from django.contrib import admin
    from django.urls import path, include, reverse
    from django.views.generic import RedirectView
    from . import views
    urlpatterns = [
        path('', views.main),
    ]
    
  4. Do not forget to add the definition for that ‘urls.py’ file of the application in the ‘urls.py’ file of the project as follows :

    from django.contrib import admin
    from django.urls import path, include, reverse
    from django.views.generic import RedirectView
    from . import views
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('sysapp', include('sysapp.urls')),
    ]
    
  5. Continue on for the files of the project. Just create a file with the name of ‘views.py’ in the application. It is actually a file defining how to display the view of the static HTML file. Below is the content of that file :

    def main(request):
        render("templates/sysapp/main.html")
    
  6. Last but not least, the following is the content of that HTML file :

    <html>
      <head>
        <title>Test</title>
      </head>
      <body>
        <h2>Hello World</h2>
      </body>
    </html>
    

Solve Error Message render() missing 1 required positional argument: ‘template_name’

The focus in this part is to search for solution. The solution is focusing on how to display a static HTML file. Rather than using just the return syntax, the above syntax is using ‘render’. In the ‘views.py’ file, there is a function with the name of ‘main’. Actually, in this application, that function is responsible for showing the output of the URL address. That HTML file in this application is a file with the name of ‘main.html’. Moreover, the location is in the folder ‘templates/sysapp’. Unfortunately, it triggers an error of improper use of the ‘render’ syntax. Basically, in order to use it correctly, it requires another argument with the name of ‘template_name’. According to the Django documentation exist in this link, it needs two argument at least. The explanation of the arguments exist in that documentation page as follows :

Required arguments

request
The request object used to generate this response.
template_name
The full name of a template to use or sequence of template names. If a sequence is given, the first template that exists will be used. See the template loading documentation for more information on how templates are found.

Knowing that, just edit the ‘main’ functions especially the ‘render’ function. Just change it as follows :

def main(request):
    render(request, "templates/sysapp/main.html")

But the above source code does not solve the problem. It will generate another error. So, instead of using the above source code to display a static HTML file, just revise the above source code as follows :

def main(request):
    return render(request, "templates/sysapp/main.html")

Execute once more, the following image will appear :

How to Solve Error Message render() missing 1 required positional argument: 'template_name' in Django Application
How to Solve Error Message render() missing 1 required positional argument: ‘template_name’ in Django Application

Leave a Reply