小编Ulf*_*orm的帖子

将带有空格的标题转换为url友好的标题,如stackoverflows标题

我有一个博客应用程序,其中包含最新的帖子标题列表.现在,列表项应链接到其内容.我的问题(类似的帖子)是,如果标题有一些空格我得到一个带空格的网址,如果我使用:

<a href="{{  i.id  }}/{{  i.title  }}">{{ i.title }}
Run Code Online (Sandbox Code Playgroud)

在我的模板中.我可以使用额外的URLField,但我不想手动创建友好的URL标题.这样做的常见方法是什么?

我的models.py

class Post(models.Model):
    title = models.CharField(max_length=100)
    ...

    def __unicode__(self):
        return self.title
Run Code Online (Sandbox Code Playgroud)

我的view.py

def recentlyBlogged(request):
    lastPosts = Post.objects.filter(publication__gt = datetime.now() - timedelta(days=30))
    return render(request, "blog/blog.html", {'Posts': lastPosts})
Run Code Online (Sandbox Code Playgroud)

我的模板

{% for i in Posts %}
    <ul id="latestPostsList">
        <li class="latestPostsListItem"><a href="{{  i.id  }}/{{  i.title  }}">{{ i }}</a></li>
{% endfor %}
    </ul>
Run Code Online (Sandbox Code Playgroud)

django django-templates django-models

1
推荐指数
1
解决办法
472
查看次数

C++:string.find仅在字符串pos1上找到字符串中的字符

我想在字符串(word)中找到一个char(expected_char)

if (word.find(expected_char)==true)
{
    cout << "You got one! It's on pos" << word.find(expected_char);
}
else
{
...
}
Run Code Online (Sandbox Code Playgroud)

如果我的字符串是例如"abcd"并且我搜索"c"则将执行其他字符串; 如果我搜索"b",将执行if语句.

c++

0
推荐指数
2
解决办法
183
查看次数

如何返回浮点数组

我需要一个函数来创建一个带有一些浮点的数组.

double * my_function( )
{
    static double arr[10] = {20, 21, 22, 23, 24, 25, 26, 27, 28, 29};

    return arr;
}


int main ()
{
    double *first_pos;
    int i;

    first_pos = my_function();
    for ( i = 0; i < 10; i++ )
    {
        printf( "%d", *(first_pos + i));
    }

return 0;
}
Run Code Online (Sandbox Code Playgroud)

这会打印一些"随机"数字.

我对指针/数组感到困惑!

c printf

0
推荐指数
1
解决办法
85
查看次数

标签 统计

c ×1

c++ ×1

django ×1

django-models ×1

django-templates ×1

printf ×1