Home > Software design >  How to import a function from a view.py file and launch a server with a basic view in django?
How to import a function from a view.py file and launch a server with a basic view in django?

Time:05-26

The server works fine when I run python3 manage.py runserver on the VSC terminal But as soon as I try to import a function from a view.py file

def hello(request):
    return HttpResponse("Hello Django - Coder")

to the urls.py file the server doesn't work

I do the from django.view import hello

and I also modify the url by adding the path "hello" of the function

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

]

But it doesn't work. Any help would be much appreciated

CodePudding user response:

I recommend you to read this doc https://docs.djangoproject.com/en/4.0/ref/urls/ It will help you better understand the logic of the "path".

In your case it should be something like:

from django.urls import path
from . import views

urlpatterns = [
    path('hello/', views.hello, name="hello"),
]

CodePudding user response:

I do the from django.view import hello

from django.views import hello

You lost a "s" after "view",

  • Related