Gopal Adhikari
Gopal Adhikari

Reputation: 21

cant get hello world in django

I have python version 3.10.1 and django version 4.0

url in project( name = home)`

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

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

url in app (name = hello)

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

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

views in app

from django.http import HttpResponse


   # Create your views here.
   def index(request):
      return HttpResponse("Hello World")

I tried running server with and without adding 'hello' in setting.py still i get only default page. stuck from 3 days

Upvotes: 0

Views: 579

Answers (2)

Nitesh Singh
Nitesh Singh

Reputation: 395

You have the same code in urls.py home project as well as in hello app. For Django to use your new view, you need to tell Django the index view is the view you want to display when someone navigates to the site root (home page). So you need to change the urls.py of hello app as:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index),
]

In this case, a request to http://localhost:8000/ would route to the index function in the application’s (hello) views.py file.

Upvotes: 1

mirodil
mirodil

Reputation: 537

In the urls.py file in app write the following code:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

Upvotes: 0

Related Questions