← Back to Django Course | Chapter 2: Apps, URLs & Views | Lesson 8 of 10

Function-Based Views का परिचय

एक function-based view सिर्फ एक normal Python function है जिसे Django तब call करता है जब कोई certain page visit करता है, और इसका job एक reply वापस भेजना है।
Syntax
markup
from django.http import HttpResponse

def view_name(request):
    # build the response
    return HttpResponse('content')

एक Basic View लिखना

एक function-based view views.py में defined एक regular Python function है। इसे अपने पहले argument के रूप में HttpRequest object accept करना चाहिए (conventionally request नाम से) और एक HttpResponse object return करना चाहिए।

उदाहरण: Writing a Basic View

A function-based view is a regular Python function defined in views.py. It must accept the HttpRequest object as its first argument (conventionally named request) and must return an HttpResponse object.

markup
from django.http import HttpResponse

def home(request):
    return HttpResponse('Welcome to my Django site!')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

View को एक URL से Connect करना

एक view function अपने आप कुछ नहीं करता जब तक एक URL pattern इसकी ओर point न करे। view module को urls.py में import करें और path() call में function reference करें।

उदाहरण: Connecting the View to a URL

A view function does nothing on its own until a URL pattern points to it. Import the view module in urls.py and reference the function in a path() call.

markup
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Views Logic पर Branch कर सकते हैं

क्योंकि एक view सिर्फ एक function है, यह regular Python control flow रख सकता है यह तय करने के लिए कि कौन सा response वापस भेजना है, जैसे request method या query parameters जांचना।

उदाहरण: Views Can Branch on Logic

Because a view is just a function, it can contain regular Python control flow to decide what response to send back, such as checking the request method or query parameters.

markup
from django.http import HttpResponse

def greet(request):
    name = request.GET.get('name', 'stranger')
    return HttpResponse(f'Hello, {name}!')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि हर view function को अपने पहले parameter के रूप में request accept करना चाहिए।
  2. एक HttpResponse (या subclass) object के बजाय एक plain Python string या None return करना।
  3. Models या helper functions को delegate करने के बजाय view के अंदर direct heavy logic करना, जो views को test करना मुश्किल बनाता है।
चैप्टर सारांश
  • एक function-based view (FBV) एक plain Python function है जो request लेता है और एक HttpResponse return करता है।
  • Views urls.py में path() entries के through URLs से connect होते हैं।
  • FBVs simple और explicit हैं, जो इन्हें class-based views सीखने से पहले एक great starting point बनाता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.