Django中的Slugfield URL实现

时间:2015-05-23 23:12:48

标签: django slug

所以,我在尝试在我的模型中对标题字段进行重击并且仍然返回正确的信息时遇到了一些困难。

目前,如果用户的帐户中的列表存在于此正则表达式下,则用户可以关注该网址:

url(r'^user/(?P<username>\w+)/list/(?P<listname>\w+)/$', mylistpage, name='lists'),

我面临的问题是用户可以拥有一个包含空格的列表,但正则表达式将其网址从其列表名称中删除。我想要实现一个slug url,但仍然可以检索正确的模型/对象信息。

我正在尝试使用一个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")
    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)

1 个答案:

答案 0 :(得分:2)

我使用django-autoslug取得了巨大的成功。您可以找到实时示例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'),
相关问题