Python 如何在django中传递URL中的特殊字符
假设我想在url中传递用户名:Python 如何在django中传递URL中的特殊字符,python,django,django-urls,Python,Django,Django Urls,假设我想在url中传递用户名: username = 'kakar@gmail.com' 因此,在URL中,如下所示: url(r'(?P<user_name>\w+)/$', 'user_related.views.profile', name='profile'), 但我有一个错误: User matching query does not exist. 因为django会自动将@转换为%40。如何将实际的用户名传递给视图?请帮我解决这个问题。谢谢大家! 使用标准urll
username = 'kakar@gmail.com'
因此,在URL中,如下所示:
url(r'(?P<user_name>\w+)/$', 'user_related.views.profile', name='profile'),
但我有一个错误:
User matching query does not exist.
因为django会自动将@
转换为%40
。如何将实际的用户名
传递给视图?请帮我解决这个问题。谢谢大家! 使用标准urllib
模块中的函数:
from urllib import unquote
user = User.objects.get(username=unquote(user_name))
顺便说一句,据我所知,url()中的正则表达式应该是
[\w@%.]+
。普通\w+
不匹配kakar@gmail.com
和kakar%40gmail.com
使用w
url(r'^url\u name/(?p[\w%-+]+)/$),url\u方法,name='end\u point\u name')
现有答案缺少一些内容,但我没有足够的代表对其进行评论或编辑。以下是一个可行的解决方案:
对于基于函数的视图:
在视图.py中
:
# this is incorrect for current versions of Django in the other answer
from urllib.parse import unquote
def profile(request, user_name):
user = User.objects.get(username=unquote(user_name))
return render(request, 'user_profile.html', {'user':user})
from urllib.parse import unquote
from django.views.generic import DetailView
class Profile(DetailView):
"""Display the user's profile"""
template_name = 'user_profile.html'
model = User
def dispatch(self, request, *args, **kwargs):
self.username = kwargs['user_name']
return super().dispatch(request, *args, **kwargs)
def get_object(self, *args, **kwargs):
try:
return User.objects.get(username=unquote(self.username))
except:
raise Http404
from django.urls import path
urlpatterns = [
path('users/<str:user_name>/', views.Profile.as_view(), name='profile'),
# ... put more urls here...
]
然后,在url.py
中,我们可以完全跳过正则表达式:
from django.urls import path
urlpatterns = [
path('users/<str:user_name>/', views.profile, name='profile'),
# ... put more urls here...
]
使用基于类的视图时,您的url.py
:
# this is incorrect for current versions of Django in the other answer
from urllib.parse import unquote
def profile(request, user_name):
user = User.objects.get(username=unquote(user_name))
return render(request, 'user_profile.html', {'user':user})
from urllib.parse import unquote
from django.views.generic import DetailView
class Profile(DetailView):
"""Display the user's profile"""
template_name = 'user_profile.html'
model = User
def dispatch(self, request, *args, **kwargs):
self.username = kwargs['user_name']
return super().dispatch(request, *args, **kwargs)
def get_object(self, *args, **kwargs):
try:
return User.objects.get(username=unquote(self.username))
except:
raise Http404
from django.urls import path
urlpatterns = [
path('users/<str:user_name>/', views.Profile.as_view(), name='profile'),
# ... put more urls here...
]
从django.url导入路径
URL模式=[
路径('users/',views.Profile.as_view(),name='Profile'),
#…在此处放置更多URL。。。
]
完美!!!我仍然收到一个错误用户匹配查询不存在
,但您的编辑帮助了我!非常感谢。然而,我只是想知道,%
在URL中有什么用,因为它只需要@
?“@”在URL()中有特殊的含义,所以在“URI”RFC中,建议将@
编码为%40
。但在现实生活中,你可以在URL路径中安全地使用@
。这个答案是个救命稻草!非常感谢。请注意,在Python3中,unquote现在位于urllib.parse,因此如果使用Python3,请将urllib import unquote的替换为urllib.parse import unquote的