Django view processes the request and returns the response. 1. The function view judges the method type through the request parameter. The GET request uses render to return the HTML page. The POST request can process the form and return JsonResponse; 2. The class view inherits the View class and defines the get and post methods to handle different requests, which is suitable for logically complex scenarios; 3. The URL route is configured through path, and the function view is directly referenced, and the class view needs to call the as_view() method; 4. The template uses {{ }} to render variables, and the form must contain {% csrf_token %} to prevent cross-site request forgery; 5. The form class inherits forms.Form definition fields, verify data after submission and returns the results. It is recommended to use common class views such as TemplateView or FormView to reduce duplicate code.

In Django, views are the core part of processing requests and returning responses. Here is a simple Django view example covering common usages: function views and class views, and how to handle GET/POST requests.

? 1. Function-Based View example
# views.py
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect
from .forms import MyForm # Suppose you have a form def home_view(request):
"""Processing homepage requests""
if request.method == 'GET':
return render(request, 'home.html', {'message': 'Welcome to homepage!'})
def submit_form(request):
"""Processing form submission"""
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
# Process data, such as saving to the database return JsonResponse({'status': 'success', 'data': form.cleaned_data})
else:
return JsonResponse({'status': 'error', 'errors': form.errors}, status=400)
return JsonResponse({'status': 'error', 'message': 'only POST requests'}, status=405)
? 2. Class-Based View example
# views.py
from django.views import View
from django.http import JsonResponse
class MyApiView(View):
def get(self, request):
return JsonResponse({'method': 'GET', 'info': 'This is a GET request'})
def post(self, request):
return JsonResponse({'method': 'POST', 'info': 'POST request received'})
# urls.py (app level)
from django.urls import path
from . import views
urlpatterns = [
path('', views.home_view, name='home'),
path('submit/', views.submit_form, name='submit'),
path('api/', views.MyApiView.as_view(), name='api'),
]
Make sure your project urls.py
contains the application route:
# project/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')), # Replace myapp with your application name]
? 4. Simple HTML template example
<!-- templates/home.html -->
<!DOCTYPE html>
<html>
<head>
<title>Homepage</title>
</head>
<body>
<h1>{{ message }}</h1>
<form action="{% url 'submit' %}" method="post">
{% csrf_token %}
<input type="text" name="name" placeholder="enter name" required>
<button type="submit">Submit</button>
</form>
</body>
</html>
# forms.py
from django import forms
class MyForm(forms.Form):
name = forms.CharField(max_length=100)
email = forms.EmailField()
? Tips
- Use
render()
to quickly return HTML templates.
-
JsonResponse
is suitable for front-end and back-end separation projects to return JSON.
- Don't forget
{% csrf_token %}
in the POST form.
- Class views are more suitable for complex logic and support more built-in methods (such as
get_context_data
).
Basically that's it. Not complicated, but covers the most commonly used scenarios for Django views. You can expand according to your needs, such as using general class views such as TemplateView
and FormView
to reduce duplicate code.

The above is the detailed content of python django views example. For more information, please follow other related articles on the PHP Chinese website!