小编Tee*_*bes的帖子

Django FileField,在运行时确定upload_to

我正在尝试设置我的上传,以便如果用户joe上传文件,则转到MEDIA_ROOT/joe,而不是让每个人的文件都转到MEDIA_ROOT.问题是我不知道如何在模型中定义它.以下是目前的情况:

class Content(models.Model):
    name = models.CharField(max_length=200)
    user = models.ForeignKey(User)
    file = models.FileField(upload_to='.')
Run Code Online (Sandbox Code Playgroud)

所以我想要的不是'.' 作为upload_to,让它成为用户的名字.

据我所知,从Django 1.0开始,您可以定义自己的函数来处理upload_to,但该函数不知道用户是谁,所以我有点迷失.

谢谢您的帮助!

python django django-models

127
推荐指数
3
解决办法
5万
查看次数

django评论:如何防止表单错误将用户重定向到预览页面?

目前,如果表单上有任何错误,django.contrib.comments会将用户发送到预览页面.

我在博客的上下文中使用评论,我宁愿用户留在他们所在的页面上,如果提交有问题.据我所知,这在django.contrib.comments.views.comments.post_comment中是硬编码的:

# If there are errors or if we requested a preview show the comment
if form.errors or preview:
    template_list = [
        "comments/%s_%s_preview.html" % tuple(str(model._meta).split(".")),
        "comments/%s_preview.html" % model._meta.app_label,
        "comments/preview.html",
    ]
    return render_to_response(
        template_list, {
            "comment" : form.data.get("comment", ""),
            "form" : form,
            "next": next,
        },
        RequestContext(request, {})
    )
Run Code Online (Sandbox Code Playgroud)

有没有什么办法可以在不将源代码更改为django.contrib.comments的情况下更改此行为?

任何指针都会被赞赏......

谢谢!

python django django-contrib

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

以纯文本形式返回 Django 异常

Django 在 DEBUG 模式下捕获的错误以 HTML 格式的代码返回以供浏览器显示。我想让这些错误以纯文本(或 JSON)显示。有没有办法做到这一点?

django debugging error-handling

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