程序设计应用2023-03-18()

在first程序的里面views.py返回

第三章 url的分发

url配置

url参数传递

反向解析url

url命名空间

虚拟路径和虚拟文件名显示给用户,这样比较安全

127.0.0.1:8000/first/ad

甚至可以这么配置,这里的views是导入的,from first import views

多个配置指向同一个,以第一个为准。

url pattern sample for re_path in Django

re_path()
re_path()
from django.urls import re_path
from . import views

# Define a URL pattern using a regular expression
urlpatterns = [
    re_path(r'^blog/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<slug>[\w-]+)/$', views.blog_post_detail),
]

In this example, the regular expression is used to match URLs that follow this pattern: , where is a four-digit year, is a two-digit month, and is a string of alphanumeric characters and hyphens.

r'^blog/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<slug>[\w-]+)/$'
/blog/YYYY/MM/slug/
YYYY
MM
slug

The regular expression uses named groups to capture the values for the , , and parameters. These values will be passed to the function as arguments when the URL is matched.

year
month
slug
views.blog_post_detail

Note that is used instead of when you need to use regular expressions to define URL patterns.

re_path()
path()

url parameter vs kwargs parameter

In Django, both URL parameters and keyword arguments (kwargs) can be used to pass data to a view function, but they are used in different contexts.

URL parameters are used to capture parts of the URL path and pass them as arguments to a view function. They are defined using angle brackets and can be of various types, including integers (), strings (), and slugs (), among others.

<>
<int:parameter_name>
<str:parameter_name>
<slug:parameter_name>

For example, in the URL pattern , the and values are extracted from the URL path and passed as arguments to the view function.

/blog/<int:year>/<slug:slug>/
year
slug

Keyword arguments, on the other hand, are used to pass named arguments to a view function. They are typically used in conjunction with the function to generate URLs dynamically based on the arguments passed to the view function.

reverse()

For example, in the following view function:

def blog_post(request, year, slug):
    # do something with the year and slug arguments
    return HttpResponse("Blog post for year {} and slug {}".format(year, slug))

We can use the function to generate a URL for this view function based on the and arguments: 

reverse()
year
slug
from django.urls import reverse

url = reverse('blog_post', kwargs={'year': 2023, 'slug': 'my-blog-post'})
# generates the URL '/blog/2023/my-blog-post/'

Overall, URL parameters are used to capture data from the URL path, while keyword arguments are used to pass data to a view function or generate URLs dynamically based on named arguments.

How url modules loaded in Django across root and sub applications?

In Django, URL modules are loaded automatically when the root URLconf module is loaded. This happens when the Django server starts up or when the module is imported by another module.

urls.py

The root URLconf module is typically named and is located in the main application directory. It contains the main URL patterns for the application and serves as the entry point for all incoming requests.

urls.py

When the Django server starts up, it looks for the module in the main application directory and loads it. The list in the root URLconf module can include patterns for the main application as well as patterns for any included sub-applications.

urls.py
urlpatterns

Sub-applications in Django are typically organized as separate modules, each with its own module that defines the URL patterns for that module. These sub-modules can be included in the root file using the function.

urls.py
urls.py
include()

For example, if we have a sub-application named , we can include its URL patterns in the root file like this:

blog
urls.py
from django.urls import include, path

urlpatterns = [
    path('', include('mainapp.urls')),
    path('blog/', include('blog.urls')),
    # other URL patterns for the main application
]

In this example, the function is used to include the URL patterns defined in the module. The module could have its own sub-modules with their own files that can be included in the module in a similar way.

include()
blog.urls
blog
urls.py
blog.urls

Overall, Django loads URL modules automatically when the root URLconf module is loaded, and sub-modules can be included in the root file using the function.

urls.py
include()

Explain the path function parameters in url patterns

In Django, the function is used to define URL patterns in the module. The function takes several parameters that define the URL pattern and any associated view function or options. The parameters for the function are as follows:

path()
urls.py
path()
path()
  • route: This is the first parameter and is a string that defines the URL pattern. It can include named groups enclosed in angle brackets (<>) to capture values from the URL. For example, the route “/edit/” matches any URL that ends in /edit/ and captures the value between the start of the URL and /edit/ as a string.
  • view: This parameter is required and specifies the view function that will be called when the URL pattern is matched. It can be a reference to a function or the name of a function as a string.
  • kwargs: This parameter is optional and can be used to pass additional keyword arguments to the view function.
  • name: This parameter is optional and provides a name for the URL pattern. It can be used to generate URLs for the pattern using the reverse() function.
  • regex: This parameter is optional and can be used to specify a regular expression to match the URL pattern instead of the route parameter.
  • unique: This parameter is optional and specifies whether the URL pattern is unique. If set to True, it means that this pattern cannot be used by any other URL pattern in the URLconf.
  • kwargs_format: This parameter is optional and specifies the format of keyword arguments that are passed to the view function. The default value is ‘string’, which means that keyword arguments are passed as strings. Other possible values are ‘path’ and ‘querystring’, which specify that keyword arguments should be passed as part of the URL path or in the query string, respectively.

Overall, the function is a flexible way to define URL patterns in Django, with options for capturing values from the URL, passing additional arguments to the view function, and generating URLs using the function.

path()
reverse()

Explain the re_path function parameters in url patterns

In Django, the function is used to define URL patterns using regular expressions. The function is similar to the function but allows for more complex URL patterns using regular expressions. The parameters for the function are as follows:

re_path()
re_path()
path()
re_path()
  • route: This is the first parameter and is a regular expression string that defines the URL pattern. The regular expression can include named groups enclosed in parentheses to capture values from the URL. For example, the route r’^articles/(?P[-\w]+)/$’ matches any URL that starts with articles/ followed by a slug value, which is captured as a string.
  • view: This parameter is required and specifies the view function that will be called when the URL pattern is matched. It can be a reference to a function or the name of a function as a string.
  • kwargs: This parameter is optional and can be used to pass additional keyword arguments to the view function.
  • name: This parameter is optional and provides a name for the URL pattern. It can be used to generate URLs for the pattern using the reverse() function.
  • prefix: This parameter is optional and specifies a prefix to be added to the regular expression. It can be used to group related URL patterns together.
  • pattern: This parameter is optional and allows for more complex regular expressions to be used for the URL pattern.
  • unique: This parameter is optional and specifies whether the URL pattern is unique. If set to True, it means that this pattern cannot be used by any other URL pattern in the URLconf.
  • kwargs_format: This parameter is optional and specifies the format of keyword arguments that are passed to the view function. The default value is ‘string’, which means that keyword arguments are passed as strings. Other possible values are ‘path’ and ‘querystring’, which specify that keyword arguments should be passed as part of the URL path or in the query string, respectively.

Overall, the function provides more flexibility than the function for defining URL patterns using regular expressions. It can be useful for handling more complex URL patterns or for grouping related patterns together using a prefix.

re_path()
path()
————————

在first程序的里面views.py返回

第三章 url的分发

url配置

url参数传递

反向解析url

url命名空间

虚拟路径和虚拟文件名显示给用户,这样比较安全

127.0.0.1:8000/first/ad

甚至可以这么配置,这里的views是导入的,from first import views

多个配置指向同一个,以第一个为准。

url pattern sample for re_path in Django

re_path()
re_path()
from django.urls import re_path
from . import views

# Define a URL pattern using a regular expression
urlpatterns = [
    re_path(r'^blog/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<slug>[\w-]+)/$', views.blog_post_detail),
]

In this example, the regular expression is used to match URLs that follow this pattern: , where is a four-digit year, is a two-digit month, and is a string of alphanumeric characters and hyphens.

r'^blog/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<slug>[\w-]+)/$'
/blog/YYYY/MM/slug/
YYYY
MM
slug

The regular expression uses named groups to capture the values for the , , and parameters. These values will be passed to the function as arguments when the URL is matched.

year
month
slug
views.blog_post_detail

Note that is used instead of when you need to use regular expressions to define URL patterns.

re_path()
path()

url parameter vs kwargs parameter

In Django, both URL parameters and keyword arguments (kwargs) can be used to pass data to a view function, but they are used in different contexts.

URL parameters are used to capture parts of the URL path and pass them as arguments to a view function. They are defined using angle brackets and can be of various types, including integers (), strings (), and slugs (), among others.

<>
<int:parameter_name>
<str:parameter_name>
<slug:parameter_name>

For example, in the URL pattern , the and values are extracted from the URL path and passed as arguments to the view function.

/blog/<int:year>/<slug:slug>/
year
slug

Keyword arguments, on the other hand, are used to pass named arguments to a view function. They are typically used in conjunction with the function to generate URLs dynamically based on the arguments passed to the view function.

reverse()

For example, in the following view function:

def blog_post(request, year, slug):
    # do something with the year and slug arguments
    return HttpResponse("Blog post for year {} and slug {}".format(year, slug))

We can use the function to generate a URL for this view function based on the and arguments: 

reverse()
year
slug
from django.urls import reverse

url = reverse('blog_post', kwargs={'year': 2023, 'slug': 'my-blog-post'})
# generates the URL '/blog/2023/my-blog-post/'

Overall, URL parameters are used to capture data from the URL path, while keyword arguments are used to pass data to a view function or generate URLs dynamically based on named arguments.

How url modules loaded in Django across root and sub applications?

In Django, URL modules are loaded automatically when the root URLconf module is loaded. This happens when the Django server starts up or when the module is imported by another module.

urls.py

The root URLconf module is typically named and is located in the main application directory. It contains the main URL patterns for the application and serves as the entry point for all incoming requests.

urls.py

When the Django server starts up, it looks for the module in the main application directory and loads it. The list in the root URLconf module can include patterns for the main application as well as patterns for any included sub-applications.

urls.py
urlpatterns

Sub-applications in Django are typically organized as separate modules, each with its own module that defines the URL patterns for that module. These sub-modules can be included in the root file using the function.

urls.py
urls.py
include()

For example, if we have a sub-application named , we can include its URL patterns in the root file like this:

blog
urls.py
from django.urls import include, path

urlpatterns = [
    path('', include('mainapp.urls')),
    path('blog/', include('blog.urls')),
    # other URL patterns for the main application
]

In this example, the function is used to include the URL patterns defined in the module. The module could have its own sub-modules with their own files that can be included in the module in a similar way.

include()
blog.urls
blog
urls.py
blog.urls

Overall, Django loads URL modules automatically when the root URLconf module is loaded, and sub-modules can be included in the root file using the function.

urls.py
include()

Explain the path function parameters in url patterns

In Django, the function is used to define URL patterns in the module. The function takes several parameters that define the URL pattern and any associated view function or options. The parameters for the function are as follows:

path()
urls.py
path()
path()
  • route: This is the first parameter and is a string that defines the URL pattern. It can include named groups enclosed in angle brackets (<>) to capture values from the URL. For example, the route “/edit/” matches any URL that ends in /edit/ and captures the value between the start of the URL and /edit/ as a string.
  • view: This parameter is required and specifies the view function that will be called when the URL pattern is matched. It can be a reference to a function or the name of a function as a string.
  • kwargs: This parameter is optional and can be used to pass additional keyword arguments to the view function.
  • name: This parameter is optional and provides a name for the URL pattern. It can be used to generate URLs for the pattern using the reverse() function.
  • regex: This parameter is optional and can be used to specify a regular expression to match the URL pattern instead of the route parameter.
  • unique: This parameter is optional and specifies whether the URL pattern is unique. If set to True, it means that this pattern cannot be used by any other URL pattern in the URLconf.
  • kwargs_format: This parameter is optional and specifies the format of keyword arguments that are passed to the view function. The default value is ‘string’, which means that keyword arguments are passed as strings. Other possible values are ‘path’ and ‘querystring’, which specify that keyword arguments should be passed as part of the URL path or in the query string, respectively.

Overall, the function is a flexible way to define URL patterns in Django, with options for capturing values from the URL, passing additional arguments to the view function, and generating URLs using the function.

path()
reverse()

Explain the re_path function parameters in url patterns

In Django, the function is used to define URL patterns using regular expressions. The function is similar to the function but allows for more complex URL patterns using regular expressions. The parameters for the function are as follows:

re_path()
re_path()
path()
re_path()
  • route: This is the first parameter and is a regular expression string that defines the URL pattern. The regular expression can include named groups enclosed in parentheses to capture values from the URL. For example, the route r’^articles/(?P[-\w]+)/$’ matches any URL that starts with articles/ followed by a slug value, which is captured as a string.
  • view: This parameter is required and specifies the view function that will be called when the URL pattern is matched. It can be a reference to a function or the name of a function as a string.
  • kwargs: This parameter is optional and can be used to pass additional keyword arguments to the view function.
  • name: This parameter is optional and provides a name for the URL pattern. It can be used to generate URLs for the pattern using the reverse() function.
  • prefix: This parameter is optional and specifies a prefix to be added to the regular expression. It can be used to group related URL patterns together.
  • pattern: This parameter is optional and allows for more complex regular expressions to be used for the URL pattern.
  • unique: This parameter is optional and specifies whether the URL pattern is unique. If set to True, it means that this pattern cannot be used by any other URL pattern in the URLconf.
  • kwargs_format: This parameter is optional and specifies the format of keyword arguments that are passed to the view function. The default value is ‘string’, which means that keyword arguments are passed as strings. Other possible values are ‘path’ and ‘querystring’, which specify that keyword arguments should be passed as part of the URL path or in the query string, respectively.

Overall, the function provides more flexibility than the function for defining URL patterns using regular expressions. It can be useful for handling more complex URL patterns or for grouping related patterns together using a prefix.

re_path()
path()