My first Django App - don't know how to create a homepage

Viewed 1165

I have been learning python for some time now and I want to make one of my small programs available to more people. I am learning how to make URLs for my project now and I cannot create a URL for a homepage. As I found in one of the courses for older django version, you should create a function in your urls.py file that looks like this:

from django.http import HttpResponse

def home(request):
    return HttpResponse('this is the test homepage')

and pair it with some lines in views.py:

path('^$', views.home)

This doesn't work for me. I also tried to create a path like this:

 path('/', views.home)

Please, help or direct me to up to date guides for django 3.1.1.

EDIT in response to @pygeek's request:

the content of my urls.py:

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('about/', views.about),
    path('/', views.home)
]

the contents of my apps array in settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

And the project tree:

.
└── RemoveBigFile
    ├── RBF1module
    │   ├── __init__.py
    │   ├── admin.py
    │   ├── apps.py
    │   ├── migrations
    │   │   └── __init__.py
    │   ├── models.py
    │   ├── tests.py
    │   └── views.py
    ├── RemoveBigFile
    │   ├── __init__.py
    │   ├── __pycache__
    │   │   ├── __init__.cpython-38.pyc
    │   │   ├── settings.cpython-38.pyc
    │   │   ├── urls.cpython-38.pyc
    │   │   ├── views.cpython-38.pyc
    │   │   └── wsgi.cpython-38.pyc
    │   ├── asgi.py
    │   ├── settings.py
    │   ├── urls.py
    │   ├── views.py
    │   └── wsgi.py
    ├── RemoveBigFile.sublime-project
    ├── RemoveBigFile.sublime-workspace
    ├── db.sqlite3
    └── manage.py
2 Answers

The 3.1. django uses an empty quote

'' 

for homepage instead of regex

^$

or slash

/

Thanks to @pygeek's contribution my code looks like this in urls.py:

path('', views.home),

and works just fine. I found the response in sample codes in the following link: docs.djangoproject.com/en/3.1/topics/http/urls

You can use HttpResponse but I find it is better to render the page. Here is a quick sample.

# urls.py
from django.urls import path
from appname import views

urlpatterns = [
   path('', views.index, name='index')
]
# views.py
from django.shortcuts import render

def index(request):
   return render(request, 'index.html')

This view will be triggered when an empty url path is requested. In the development environment this would be: http://localhost:8000/ This assumes you have also created a templates directory in your project root. You can verify this in settings.py.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [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',
            ],
        },
    },
]

Take note of the 'DIRS' which is pointing to the base directory, then looking for a directory called 'templates'. This directory will be the home of the templates rendered when the view is called. In views.py notice 'index.html' this is the template the view will render when the function is called.

Another important point. For each app you create with python manage.py startapp appname you should create a python file called urls.py within that app directory. In order for this to work you should modify your main urls.py to look something like this:

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

urlpatterns = [
    path('', include('appname.urls')),
    path('admin/', admin.site.urls),
]
Related