3👍
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
1👍
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),
]
- [Django]-__init__() got an unexpected keyword argument 'mime' in python/django
- [Django]-Django ignoring translated strings from third party module