Geovanna Vitorya
Geovanna Vitorya

Reputation: 1

I can't create a new url on django

i've watched some tutorials on how to create a new simple url and for some reason it doesn't work, it looks like i didnt registred any urls even though i did.

i created an app called 'Login' and registered it in the "INSTALLED APPS" list on settings.py of django :

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

then i created the function called 'home' on the views file of the app, to show a phrase on the page:

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

def home(request):
    return HttpResponse('Hello')

and then i created a new url path on the urls.py file of the project, without a name at first, just (""):

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

from Login.views import home

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

]

and when i use the (python manage.py runserver) command it just got back to the "success install" page of django, the one with the little rocket.

Then I tried using a name for the url ("home/"):

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

from Login import views

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

]

but when i access the (localhost:8000/home/) it says that such url wasn't found, thats the message i get from the page:

Page not found (404)

Request Method:GETRequest URL:http://127.0.0.1:8000/home/

Using the URLconf defined in setup.urls, Django tried these URL patterns, in this order:

  1. admin/

The current path, home/, didn’t match any of these.

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

And thats the message i get from the terminal:

19/Jan/2025 11:33:11] "GET / HTTP/1.1" 200 12068

Not Found: /home/

[19/Jan/2025 11:33:16] "GET /home/ HTTP/1.1" 404 2189

Not Found: /home/

[19/Jan/2025 11:33:17] "GET /home/ HTTP/1.1" 404 2189

I also tried to put the DEBUG on False but it didnt changed nothing. I also tried creating a urls.py file on the app folder but it also didnt help. Please help me lol, im not a fluent in english so i hope i explained it well.

Upvotes: 0

Views: 59

Answers (1)

dev_light
dev_light

Reputation: 3899

You would have to 'include' your app in the project's root urls.py (where you have your admin URLConf. This is your project level urls.py and it's usually found in the same directory with settings py. It should look like this:

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

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

]

Then in app level URLs.py (the one you created) usually found in your app directory:

from django.urls import path

from . import views

urlpatterns = [
    path('home/', views.home)

]

Now run python manage.py runserver and your website should be available at localhost:8000/home/. See the docs.

Upvotes: 0

Related Questions