Django not recognizing empty url config

Viewed 291

Sorry, beginner here. I was following this tutorial https://www.youtube.com/watch?v=JD-age0BPVo on py/django and managed to get the webserver running. However, it simply displays the default page: enter image description here

when in my urls.py, I have the default page set.

urls.py

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

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

and in api.urls:

from django.urls import path
from .views import main

urlpatterns = [
    path('', main)
]

and finally in api.views.py I have main(request)

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.

def main(request):
    
    return HttpResponse("hello")

My project directory looks like this in case it helps:

enter image description here

EDIT:

Doesnt work even when adding directory: enter image description here

3 Answers

Edit:

Your files are not saved in VS Code, see the white "dot"/circle on almost each file's tab. Find how to save a file in that editor (probably Ctrl + S), then restart the server, then check the website.

It's also confirmed by the 404 page listing all of the paths - yours not being even present.


I've just tried the same code and it works just fine with Django 3.2.4, so there's no problem with using path("", ...) nor including these patterns to the project's urls.py

I've started the server with:

 python manage.py runserver
 curl http://localhost:8000
  1. make sure there isn't any api module already in your Python installation (import api then check what's the contents of the module dir(api))
  2. make sure you have all of the files in VS code properly saved
  3. make sure you've restarted the server if it hasn't picked up the file changes
  4. check if you're using the correct port / if there isn't a different (older) instance of this application running already
  5. make sure your browser isn't caching the website (Ctrl + Shift + R on Windows/Linux or Cmd + Shift + F5 on MacOS)

What is the problem? Are you getting an error? Did you add your app name to Django settings? If you have not done so, enter the main app settings of the project and put your app name between the two '' in the INSTALLED_APPS section.

For example

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

]

your app name is 'api' and 'tts' is you main app for project and go to setting and insert api

settings.py

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

 ]

This is your current view address : 127.0.0.1:8000/api/.

Of course, if you want to create the main page of the site, you have to create your 'tts' 'url' section in urls.py

urls.py in tts

 from django.contrib import admin
 from django.urls import include 
 from django.urls import path
 from api.views import main

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