Django 中的 Slugfield URL 实现
Slugfield URL implementation in Django
所以,我在尝试对我的模型中的标题字段进行 slugify 并且仍然拥有 return 正确信息时遇到了一些困难。
目前,如果用户帐户中的列表存在于此正则表达式下,则用户可以关注 url:
url(r'^user/(?P<username>\w+)/list/(?P<listname>\w+)/$', mylistpage, name='lists'),
我面临的问题是用户可以有一个包含空格的列表,但正则表达式将他们的 url 基于他们的列表名称。我想实现一个 slug url,但仍然让它检索正确的 model/object 信息。
我试图有一个 slug 字段,然后 pre-populate 它基于列表名称,但我不知道这个实现应该如何工作。任何有见识的人都提前表示感谢。
型号
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")
slugurl = models.SlugField(default = slugurl(self))
def __str__(self):
return self.list_name
def slugurl(self):
return slugify(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)
我用过django-autoslug to great success. You can find a live example here.
SlugField 只是一个带有 little syntactic sugar.
的字符字段
您需要将您的 slug 命名为 slug 以便 django 可以在 URL 分辨率中自动找到它并将正确的参数传递给视图。
您修改后的代码如下所示:
from autoslug import AutoSlugField
from django.db import models
class Newlist(models.Model): # Classes start with uppercase names by default
user = models.ForeignKey(User)
list_name = models.CharField(max_length = 100,)
picture = models.ImageField(upload_to='profiles/', default = "/media/profiles/default.jpg")
slug = AutoSlugField(populate_from='list_name')
def __str__(self):
return self.list_name
您的观点:
def mylistpage(request,username, slug):
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(slug=slug)
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)
urls.py
url(r'^user/(?P<username>\w+)/list/(?P<slug>[\w-]+)/$', mylistpage, name='lists'),
所以,我在尝试对我的模型中的标题字段进行 slugify 并且仍然拥有 return 正确信息时遇到了一些困难。
目前,如果用户帐户中的列表存在于此正则表达式下,则用户可以关注 url:
url(r'^user/(?P<username>\w+)/list/(?P<listname>\w+)/$', mylistpage, name='lists'),
我面临的问题是用户可以有一个包含空格的列表,但正则表达式将他们的 url 基于他们的列表名称。我想实现一个 slug url,但仍然让它检索正确的 model/object 信息。
我试图有一个 slug 字段,然后 pre-populate 它基于列表名称,但我不知道这个实现应该如何工作。任何有见识的人都提前表示感谢。
型号
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")
slugurl = models.SlugField(default = slugurl(self))
def __str__(self):
return self.list_name
def slugurl(self):
return slugify(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)
我用过django-autoslug to great success. You can find a live example here.
SlugField 只是一个带有 little syntactic sugar.
的字符字段您需要将您的 slug 命名为 slug 以便 django 可以在 URL 分辨率中自动找到它并将正确的参数传递给视图。
您修改后的代码如下所示:
from autoslug import AutoSlugField
from django.db import models
class Newlist(models.Model): # Classes start with uppercase names by default
user = models.ForeignKey(User)
list_name = models.CharField(max_length = 100,)
picture = models.ImageField(upload_to='profiles/', default = "/media/profiles/default.jpg")
slug = AutoSlugField(populate_from='list_name')
def __str__(self):
return self.list_name
您的观点:
def mylistpage(request,username, slug):
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(slug=slug)
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)
urls.py
url(r'^user/(?P<username>\w+)/list/(?P<slug>[\w-]+)/$', mylistpage, name='lists'),