如何添加slugified字段

发布于 2024-10-03 23:55:22 字数 545 浏览 3 评论 0原文

在创建博客时,我使用以下模型类和表单。但是由于我不希望用户自己添加 url(slugified 字段),我陷入困境,如何在保存模型之前添加 slugified url,应该是如果我是正确的,就在视图中完成。 PS:我正在使用应用程序引擎,听说 slug 字段不可用。

  class Post(db.Model):
          title=db.StringProperty(required=True)
            url=db.StringProperty(required=True)
            content_html=db.TextProperty(required=True)
            dateTime=db.DateTimeProperty(auto_now_add=True,required=True)
            tags=db.StringListProperty()


class PostForm(djangoforms.ModelForm): 
 class Meta:
  model=Post
  exclude=['url']

while creating a blog i am using the following model class and form .but since i don't want the user to add the url(slugified field) himself i am stuck how can i add the slugified url before saving the model,should it be done in the view if i am correct.
PS: i am using app engine where i heard that the slug fields aren't available.

  class Post(db.Model):
          title=db.StringProperty(required=True)
            url=db.StringProperty(required=True)
            content_html=db.TextProperty(required=True)
            dateTime=db.DateTimeProperty(auto_now_add=True,required=True)
            tags=db.StringListProperty()


class PostForm(djangoforms.ModelForm): 
 class Meta:
  model=Post
  exclude=['url']

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独岁月 2024-10-10 23:55:22

您可以在视图中执行此操作,也可以覆盖表单的保存方法。如果您在视图中执行此操作,它将看起来像这样:

#views.py
from django.template.defaultfilters import slugify

def post_create(request, ...):
    ...
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            title = form.cleaned_data['title']
            slugified_title = str(slugify(title))
            post.url = [modify the slugified_title however you want...]
            post.save()
    ...

或者,您可以覆盖表单的保存方法,该方法看起来像这样:

#forms.py
class PostForm(django.forms.ModelForm):
    class Meta:
        model=Post
        exclude=['url']
    def save(self, commit=True, force_insert=False, force_update=False):
        post = super(PostForm, self).save(commit=False)
        slugified_title = str(slugify(post.title))
        post.url = [modify the slugfield_title however you want...]
        post.save()

You could either do this in your view or override your form's save method. If you do it in your view it will look something like this:

#views.py
from django.template.defaultfilters import slugify

def post_create(request, ...):
    ...
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            title = form.cleaned_data['title']
            slugified_title = str(slugify(title))
            post.url = [modify the slugified_title however you want...]
            post.save()
    ...

Alternatively, you can override your form's save method which would look something like:

#forms.py
class PostForm(django.forms.ModelForm):
    class Meta:
        model=Post
        exclude=['url']
    def save(self, commit=True, force_insert=False, force_update=False):
        post = super(PostForm, self).save(commit=False)
        slugified_title = str(slugify(post.title))
        post.url = [modify the slugfield_title however you want...]
        post.save()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文