离开 PHP 之前的三个问题:表单集、数据源和远程身份验证

发布于 2024-08-28 11:47:34 字数 496 浏览 0 评论 0原文

我已经使用 CakePHP 很长时间了,并且对用它开发网站感到很舒服。然而,我确实更喜欢 Python,而不是 PHP,并且想迁移到 Django。

编辑:分成三个单独的问题

如何以一种形式混合多个模型?我知道表单集用于此目的,但我找不到关于此的像样的教程(视图+模板)。在 Cake 中,我可以在我的视图(模板)中简单地实现这一点:

echo $this->Form->input('User.title');
echo $this->Form->input('Profile.website');
echo $this->Form->input('Comment.0.title');
echo $this->Form->input('Comment.1.title');

这将混合用户模型、配置文件模型并在一种形式中添加两个注释。如何用 Django 做到这一点?

谢谢!

I've been using CakePHP for a long time now and feel comfortable developing sites with it. I do however like Python more then PHP and would like to move to Django.

EDIT: split in three separate questions

How can I mix multiple models in one form? I know formsets are used for this, but I can't find a decent tutorial on this (views + template). In Cake I can simply to this in my view (template):

echo $this->Form->input('User.title');
echo $this->Form->input('Profile.website');
echo $this->Form->input('Comment.0.title');
echo $this->Form->input('Comment.1.title');

This would mix a User model, a Profile model and add two comments in one form. How to do this with Django?

Thanks!

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

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

发布评论

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

评论(1

吻风 2024-09-04 11:47:34

对于第 1 部分,这比您想象的要容易得多:

forms.py:

class UserForm(forms.ModelForm):
    pass ## Using pass so I don't have to write the whole thing.

class ProfileForm(forms.ModelForm):
    pass

class CommentForm(forms.ModelForm):
    pass

views.py:

def view_forms(request):
    userform = UserForm()
    profileform = ProfileForm()
    comment1 = CommentForm()
    comment2 = CommentForm()
    if request.method = "POST":
        ## Process forms here.  Yes, I'm lazy.
    return render_to_response("template.html",
                locals(),                       
                context_instance=RequestContext(request))

template.html:

<form method="POST">
    {{ userform.as_p }}
    {{ profileform.as_p }}
    {{ comment1.as_p }}
    {{ comment2.as_p }}
</form>

In regards to part 1, this is much easier than you may think:

forms.py:

class UserForm(forms.ModelForm):
    pass ## Using pass so I don't have to write the whole thing.

class ProfileForm(forms.ModelForm):
    pass

class CommentForm(forms.ModelForm):
    pass

views.py:

def view_forms(request):
    userform = UserForm()
    profileform = ProfileForm()
    comment1 = CommentForm()
    comment2 = CommentForm()
    if request.method = "POST":
        ## Process forms here.  Yes, I'm lazy.
    return render_to_response("template.html",
                locals(),                       
                context_instance=RequestContext(request))

template.html:

<form method="POST">
    {{ userform.as_p }}
    {{ profileform.as_p }}
    {{ comment1.as_p }}
    {{ comment2.as_p }}
</form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文