0

I'm new into creating projects in Python using Django.

I'm creating a website in Python using the Django framework. I've created an "index" function in the "views.py" file to render the "index.html" file present in the "templates" folder. Below is the code for the "views.py" file.

from django.http import HttpResponse
from django.shortcuts import render


# Create your views here.
def index(request):
    return render(request,"index.html",{})

I've also added the navigation for the "index" page. Below is the code for "urls.py" file.

from django.contrib import admin
from django.urls import path

from gallery import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/', views.index,name="index")
]

Below is the code of templates section in settings.py file.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates'),], #path to templates folder in local system.
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

But when I try to open the "index" page URL, I'm not able to view the page.

Error screenshot for reference

enter image description here

Folder structure for reference:-

Gallery_Website
|--gallery
  |_ __init__.py
  |_ admin.py
  |_ apps.py
  |_ models.py
  |_ tests.py
  |_ views.py
|--Gallery_Website
  |_ __init__.py
  |_ settings.py
  |_ urls.py
  |_ wsgi.py
|--templates
  |_ index.html
|--db.sqlite3
|--manage.py

What could be wrong while defining the navigation for the "index" page?

Let me know if any other information is required.

4
  • Did you try to execute ' return render(request = request, template_name = 'index.html'' ' ? Some times, atleast in some of the versions you need to specify the parameter name in order to get the result in render() function Also, did you make sure that the index.html file is in the same directory as the views.py file ? Commented Nov 10, 2019 at 8:16
  • Please Clarify what are you getting on that page a ''error'' or something like that? Commented Nov 10, 2019 at 8:41
  • what is content of your index.html file and make sure that you have created templates folder in your gallery app and inside you have index.html page. Commented Nov 10, 2019 at 8:44
  • I've edited the above question for your reference. Commented Nov 10, 2019 at 10:20

2 Answers 2

1
In your settings.py file, add a value to you template dictionary key -- DIRS=[]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates'),],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

then create a folder "templates" where "manage.py" file resides. And in that templates folder, create a file "index.html". Then you will be a able to see your html page.
Sign up to request clarification or add additional context in comments.

4 Comments

Hi kumarm5, I've edited the post for your reference above.
It seems like your requested url is : - '127.0.0.1:8000'. So just need to change the requested url to :- 127.0.0.1:8000/index (url ends with slash)
Is there any way if the page can be viewed when I hit '127.0.0.1:8000' url ?
Yes, you can import re_path and do that like this:- from django.urls import path, re_path urlpatterns = [ path('admin/', admin.site.urls), re_path('^', views.index,name="index") ]
1

You have to remove 'index/' in urls.py

Correct Code:

urlpatterns = [
     path('admin/', admin.site.urls),
     path('', views.index,name="index")
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.