1

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

Peter Badida
  • 11,310
  • 10
  • 44
  • 90
jeb2
  • 179
  • 1
  • 6
  • Does this answer your question? [Why don't other programs see the changes I made to a file in VS Code until I save those changes?](https://stackoverflow.com/questions/76984829/why-dont-other-programs-see-the-changes-i-made-to-a-file-in-vs-code-until-i-sav) – starball Aug 26 '23 at 22:13

3 Answers3

2

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)
Peter Badida
  • 11,310
  • 10
  • 44
  • 90
  • 0. I tried import api, and it doesn't seem like there was any module named API. I tried all the other things you mentioned but it still doesn't work. something interesting I found though, was that when I entered a nonempty directory, django doesn't seem to recognize it as well (see edit in main post.) – jeb2 Jun 08 '21 at 07:25
  • Happens to everyone. Try using [the autosave](https://stackoverflow.com/a/59500985/5994041). – Peter Badida Jun 08 '21 at 07:54
2

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',

]
Mr13
  • 21
  • 2
  • The problem is that the default page doesn't recognize the URL I set it to. I tried adding my app name, which should be "tts", into the INSTALLED_APPS section, but it's still just showing the default django launch page when I do python3 manage.py runserver – jeb2 Jun 08 '21 at 07:36
0

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)
]
Mr13
  • 21
  • 2