how many types supported in url path

There are several types that can be used in URL patterns in Django. Here are some of the most commonly used types:

str: Matches any non-empty string, excluding the path separator (/).int: Matches any positive integer.slug: Matches any slug string consisting of ASCII letters or numbers, plus hyphens and underscores.uuid: Matches a UUID in hexadecimal format, with or without hyphens.path: Matches any non-empty string, including the path separator (/).You can also define your own custom types by subclassing django.urls.converters.StringConverter and implementing the to_python and to_url methods.

from django.urls import path

from . import views

urlpatterns = [

path('post//', views.post_detail, name='post_detail'),

path('product//', views.product_detail, name='product_detail'),

]

# views.py

from django.shortcuts import render, get_object_or_404

from .models import Post, Product

def post_detail(request, slug):

post = get_object_or_404(Post, slug=slug)

return render(request, 'blog/post_detail.html', {'post': post})

def product_detail(request, uuid):

product = get_object_or_404(Product, uuid=uuid)

return render(request, 'shop/product_detail.html', {'product': product})

还可以用正则

   re_path(r'^category/(?P[\w-]+)/$', views.category_detail, name='category_detail'),

def category_detail(request, category_name_slug):    category = get_object_or_404(Category, slug=category_name_slug)    return render(request, 'shop/category_detail.html', {'category': category})

 

Is uuid same as guid in C#?

UUID and GUID are similar concepts, but they are not exactly the same. UUID stands for "Universally Unique Identifier" and is a 128-bit value that is used to identify information in computer systems. GUID stands for "Globally Unique Identifier" and is a Microsoft implementation of UUIDs.

Both UUIDs and GUIDs are used to generate unique identifiers for objects, but the specific algorithms used to generate them may differ. Additionally, UUIDs are used in a variety of systems, while GUIDs are primarily used in Microsoft technologies like C#.

 

reverse url parse

The reverse function from django.urls is used to reverse URL patterns. It is useful when you want to create a URL based on a view name and optional parameters.

For example, if you have a URL pattern named my_view in your urls.py file, you can use reverse('my_view') to get the URL for that view. If the URL pattern has parameters, you can pass them as arguments to reverse. For example, if the URL pattern is my_view//, you can use reverse('my_view', args=[1]) to get the URL for my_view/1/.

You can also use keyword arguments to pass parameters to reverse. For example, if the URL pattern is my_view//, you can use reverse('my_view', kwargs={'slug': 'my-slug'}) to get the URL for my_view/my-slug/.

 

In summary, reverse is useful when you want to create a URL based on a view name and optional parameters.

 

args and kwargs in reverse url parse

In the reverse function of Django, args and kwargs are used to pass arguments to the URL pattern.

args is used to pass non-keyworded arguments to the URL pattern. For example, if you have a URL pattern like this:

path('myapp//', views.my_view, name='my_view')

You can pass the value of pk using args like this:

url = reverse('my_view', args=[1])

kwargs is used to pass keyworded arguments to the URL pattern. For example, if you have a URL pattern like this:

path('myapp///', views.my_view, name='my_view')

You can pass the values of year and month using kwargs like this:

url = reverse('my_view', kwargs={'year': 2022, 'month': 12})

 

模型项目 ORM

SQLite and MySQL are both relational database management systems, but they have some differences.

SQLite is a file-based database management system that is lightweight and easy to use. It is ideal for small-scale applications that do not require a lot of concurrent users or complex queries. SQLite is also a good choice for mobile applications because it is self-contained and does not require a separate server.

MySQL, on the other hand, is a client-server database management system that is designed for larger-scale applications. It is more powerful and scalable than SQLite and can handle more concurrent users and complex queries. MySQL is also a good choice for web applications because it can be easily integrated with web servers like Apache and Nginx.

In terms of Django, both SQLite and MySQL are supported. SQLite is the default database backend for Django, but you can easily switch to MySQL by changing the 

DATABASES

 setting in your project's settings.py file.

 

相关文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。