Edamame
Edamame

Reputation: 25366

Django: how to access functions in the views.py

I have a few functions in the view.py in my Django project:

Here is the views.py and urls.py under polls:

polls/views.py

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


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

def search(request):
    return HttpResponse("You're at the polls search.")

polls/urls.py

from django.urls import path

from . import views
from django.conf.urls import  include, url

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

I am able to get the index page, but have trouble to reach the page in the search function. I got the error below:

enter image description here

How do I access the search function in the views.py? Thanks!

Upvotes: 0

Views: 447

Answers (1)

Ehsan Nouri
Ehsan Nouri

Reputation: 2040

edit your polls/urls.py as below:

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

the first argument of the path is the url pattern.

I think you misunderstood the third argument(name). it has nothing to do with the url pattern, it's a name for the url, that'll be useful for url reversing. read the document for more information

Upvotes: 1

Related Questions