程序设计应用 2023-03-25

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/<slug:slug>/', views.post_detail, name='post_detail'),
    path('product/<uuid:uuid>/', 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<category_name_slug>[\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/<int:id>/, 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/<slug:slug>/, 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/<int:pk>/', 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/<int:year>/<int:month>/', 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.

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-03-25 az-900 考前辅导以及知识点总结
2021-03-25 Azure support plans
2019-03-25 cannot open window service on computer '.' in window application
2016-03-25 Base Class Doesn't Contain Parameterless Constructor?
2016-03-25 Detach a Database
2015-03-25 DbProviderFactories.GetFactoryClasses
2015-03-25 使用SqlTransaction回滚事务
点击右上角即可分享
微信分享提示