Django URL基于带空格的字符域?
我目前有一个基于用户输入的列表名称的url。列出你的名字。Everything查询并显示无问题。然而,当用户输入一个空格作为列表名时,浏览器并不能很好地处理它。我已经意识到我需要对字符串进行段塞,但不确定如何执行此实现。我有一种感觉,我在模型中添加了一些slug字段,然后查询slug字段名称,将其自身与呈现到页面的模型对象相关联。我只是不知道该怎么编码 模型 看法 统一资源定位地址Django URL基于带空格的字符域?,django,Django,我目前有一个基于用户输入的列表名称的url。列出你的名字。Everything查询并显示无问题。然而,当用户输入一个空格作为列表名时,浏览器并不能很好地处理它。我已经意识到我需要对字符串进行段塞,但不确定如何执行此实现。我有一种感觉,我在模型中添加了一些slug字段,然后查询slug字段名称,将其自身与呈现到页面的模型对象相关联。我只是不知道该怎么编码 模型 看法 统一资源定位地址 url(r'^user/(?P\w+)/list/(?P\w+/$),mylistpage,name='lists
url(r'^user/(?P\w+)/list/(?P\w+/$),mylistpage,name='lists'),
URL中的空格不是一个好主意。
这个用例是一个典型的应用程序候选
slug是某事物的短标签,只包含字母、数字、下划线或连字符。它们通常用于URL
因此,基本上,在您的模型中,您将添加另一个名为slug
的字段,并将其传递到URL中
您可以使用一个名为auto generate Slug的现成软件包
以下是一些文章,可能会提供更多关于slug的见解:
- URL中的空格不是一个好主意。
这个用例是一个典型的应用程序候选
slug是某事物的短标签,只包含字母、数字、下划线或连字符。它们通常用于URL
因此,基本上,在您的模型中,您将添加另一个名为
slug
的字段,并将其传递到URL中
您可以使用一个名为auto generate Slug的现成软件包
以下是一些文章,可能会提供更多关于slug的见解:
class newlist(models.Model):
user = models.ForeignKey(User)
list_name = models.CharField(max_length = 100,)
picture = models.ImageField(upload_to='profiles/', default = "/media/profiles/default.jpg")
def __str__(self):
return self.list_name
def mylistpage(request, username, listname):
context = RequestContext(request)
#make sure that the user is authenticated
if username == request.user.username:
#If the user is authenticated, then perform the following functions to the page
if request.user.is_authenticated():
#Store the current user request object into a variable
user = User.objects.get(username=username)
#Store the list name to the item that starts with the url input
listname = request.user.newlist_set.filter(list_name__iexact=listname)
listitems = request.user.newlist_set.all()
if not listname:
return redirect('/notfound')
else:
return redirect('/notfound')
return render_to_response('listview.html', {'lista': listname}, context)
url(r'^user/(?P<username>\w+)/list/(?P<listname>\w+)/$', mylistpage, name='lists'),