Function-Based Views का परिचय
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.
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.
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.
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. #}
- यह भूल जाना कि हर view function को अपने पहले parameter के रूप में
requestaccept करना चाहिए। - एक HttpResponse (या subclass) object के बजाय एक plain Python string या None return करना।
- 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 बनाता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: