DEV Community

Dr. Azad Rasul
Dr. Azad Rasul

Posted on

4- Django: Write a view

Open the file polls/views.py For example, we open "Sublime Text" software. From File>New Folder> we select: PollProject folder

Then put the following Python code in polls/views.py:

from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, visiter. You're at the polls project.")
Enter fullscreen mode Exit fullscreen mode

To call the view, we need to map it to a URL.

In the polls directory, create a file called urls.py select: polls>right click> New File> Save as> urls.py

In the polls/urls.py file include the following code:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]
Enter fullscreen mode Exit fullscreen mode

Then open: PollProject/urls.py and past the following code in it: \` from django.contrib import admin from django.urls import include, path

urlpatterns = [ path('polls/', include('polls.urls')), path('admin/', admin.site.urls), ] Verify it’s working with the following command: py manage.py runserver `\

Then, go to http://localhost:8000/polls/ in your browser.

If you like the content, please SUBSCRIBE to my channel for the future content

Top comments (0)