Redirection and application of views in django
1. In Django, we can use view redirection to jump the page displayed on the client to a different URL. For example, if we now want to link to the home page of Fujian Normal University Union College (cuc NO1) on the existing page (index1.html), how to achieve this?
1 Add a link li to the page html file (Figure 1)
Figure 1 Add link module to the original page
figure 2
2 After running, observe the URL file address displayed after clicking cuc NO1 on the page (Figure
2) Because the server side has not yet written the view function code, the page cannot be displayed temporarily. The test here mainly observes the URL address displayed on the browser
Figure 3
3
Implement redirection in views.py (from django.shortcuts import HttpResponseRedirect
Import the module) and then define the function in the view (Figure 3)
image 3
Add the corresponding path (Figure 3-) and the function mapping in the view (Figure 4) to urls.py in the Django architecture on the server side Figure 4
Complete the above steps to redirect the page to the home page of Fujian Normal University Union College
In summary, in Django, HttpResponseRedirect
is a class used to perform view redirection. It is part of the django.http
module
The HttpResponseRedirect
class is used as follows:
from django.http import HttpResponseRedirect def my_view(request): # Function other business functions #Create an HttpResponseRedirect object and specify the redirected URL return HttpResponseRedirect('/another-url/')
HttpResponseRedirect accepts a parameter /another-url/, which is the redirected target URL. In the above redirection to the homepage of Fujian Normal University Union College, the /another-url/ parameter is 'https://cuc.fjnu.edu.cn'. It should be noted that
HttpResponseRedirect requires Provide the complete URL path, including protocol, host, and path.
HttpResponseRedirect can only perform simple redirection, it does not support passing additional parameters or query strings. If you need to pass parameters when redirecting, it is recommended to use the
redirect()` function or use a query string in the target URL.
2. The redirect()
function provides a more flexible and convenient redirection method.
Django provides a convenient redirect()
function to implement redirection. Here’s a simple example:
from django.shortcuts import redirect def my_view(request): # Other functions of the function... # Redirect to another function in views.py return redirect('another_view_name')
When using the redirect()
function, you can pass the name of the target view to the function as a string. The following is part of the code in an example views.py view:
from django.shortcuts import redirect def my_view(request): # Redirect to the view named "my_target_view" return redirect('my_target_view_name') # my_target_view_name is the name parameter value in the path defined in urls.py def my_target_view(request): # Logic of target view return HttpResponse("This is my target view")
In the above example, we defined a view function named my_view
. When the user accesses my_view
, it will use the redirect()
function to redirect the user to the view named my_target_view
.
In the urls.py configuration, our target view my_target_view
can provide a corresponding URL mapping.
from django.urls import path from my1 import views # Import the view file views from the app application my1 urlpatterns = [ path('my-view/', my_view, name='my_view'), path('my-target-view/', my_target_view, name='my_target_view_name'), #name='my_target_view_name must be consistent with the one defined in the view function my_view ]
In the above URL configuration, the my_view
function in the default views will handle requests for the /my-view/
path, and the function my_target_view
will handle /my-target-view/
path request.
But when accessing /my-view/
, the my_view
view function uses redirect('my_target_view_name')
to redirect the request to
If we still take Fujian cuc NO1 as an example above, when we click cuc NO2 in Figure 5, we will be redirected to the URL corresponding to cuc NO1.
Figure 5
Code in views.py (Figure 6)
Figure 6 Use the name value in path as a parameter of redirect()
Figure 7 path settings in urls.py
The above example implements URL redirection between different view functions.
Of course, like HttpResponseRedirect, you can pass the target URL directly to the redirect()
function instead of the name of the view:
from django.shortcuts import redirect def my_view(request): # Execute some logic... # Redirect to another URL return redirect('/another-url/')
Additionally, you can pass additional parameters using named parameters:
from django.shortcuts import redirect def my_view(request): # Other functions of the function... # Redirect to another URL and pass additional parameters return redirect('another_view_name', arg1='value1', arg2='value2')
In the above example, arg1
and arg2
are parameters accepted by the target view, and these parameters will be appended to the redirected URL as query strings.
There is also a redirection class RedirectView
in django
Both the RedirectView
class and the redirect()
function can be used to implement view redirection, but there are some differences between them:
-
Class vs function:
RedirectView
is a class-based view and needs to be converted into a callable view function using theas_view()
method in the URL configuration. Theredirect()
function is a function that can be used directly in the view function. -
Configuration method:
RedirectView
defines redirection through URL configuration, and the URL pattern needs to be specified in theurls.py
file. Theredirect()
function is called inside the view function, and redirection can be flexibly performed in the view function. -
Additional features:
RedirectView
provides more properties and methods to customize redirection logic, such as thepermanent
attribute for specifying the redirection type (permanent or temporary),The get_redirect_url()
method is used to customize the generation logic of the target URL, etc. In contrast, theredirect()
function is relatively simple and is mainly used to perform simple redirections. -
Parameter passing:
RedirectView
allows the target URL to be specified directly in the URL configuration, or the target URL can be dynamically generated by overriding theget_redirect_url()
method. Theredirect()
function allows the target URL to be passed as a parameter, and additional parameters and query strings can also be passed.
To sum up, the RedirectView
class is suitable for redirection scenarios that require more flexibility and custom functions, while the redirect()
function is suitable for simple redirection needs. HttpResponseRedirect is generally only used for complete URL redirection.