【问题标题】:Edit form in Django 1.6在 Django 1.6 中编辑表单
【发布时间】:2015-05-29 07:27:50
【问题描述】:

我正在尝试使用“编辑”按钮编辑模型表单,但表单未更新。我有一个视图功能,原始用于发布和保存新帖子。然后我有第二个函数edit_comment,用于更新原始创建的对象one_entry。在我的模板中,我有一个用于提交帖子的表单和一个连接到 url“/edit”的“编辑”按钮,用于更新带有 url“/entry”的表单中添加的 one_entry 对象。非常感谢任何帮助。

在视图中

# function for adding a new post
def original(request, postID):
    one_entry = Entry.objects.get(id=postID)

    if request.method == 'POST':
        form = ReplyForm(request.POST)
        if form.is_valid():
            postOneComment(request, one_entry)

    else:
        form = ReplyForm()

    c = {"one_entry":one_entry,"form":form,"comment_views":comment_views}
    c.update(csrf(request))

    return render(request,"template.html", c)


# function for editing/updating the object one_entry
# Comment is a model and content and title are fields in the Modelform 
# that should be updated
def edit_comment(request, one_entry):
    content = Comment.objects.get(pk=one_entry)
    title = Comment.objects.get(pk=one_entry)

    if request.method == 'POST':
        form = ReplyForm(request.POST, instance=content)
        form = ReplyForm(request.POST, instance=title)
        if form.is_valid():
            postOneComment(request, one_entry)

    else:
        form = ReplyForm()

    c = {"form":form,"one_entry":one_entry}
    c.update(csrf(request))

    return render(request,"template.html", c)


def postOneComment(request, one_entry):
    content = request.POST["content"]

    title = request.POST["title"]



    user= request.user.username


    entryComment = Comment(datetime=datetime.datetime.now(), belongsTo=one_entry,content=content, title=title, user=user)
    entryComment.save()

在表格中

class ReplyForm(forms.ModelForm):
    title = forms.CharField(max_length=70)
    content = forms.CharField(widget=forms.Textarea)

    class Meta:
        model = Comment
        fields = ("title","content", "user")

在网址中

# url for adding a post to object one_entry 
url(r'^entry(?P<postID>\d+)$', original),

# url for editing/updating one_entry  
url(r'^edit(?P<one_entry>\d+)$',edit_comment),

在模板中

#button for editing one_entry
{% for t in one_entry.comment_set.all %}
    <a type="button" href="/edit{{ t.id}}" class="btn btn-xs   btn-success">EDIT</button></a>

    #form for adding  a new post
    <form  method="post" action="">{% csrf_token %}
        <center>Titel (optional){{ form.title}}</center></br>
        <center>{{ form.content}}</center>
        <center><button type="submit" class="btn btn-success">POST</button></center>
    </form>

【问题讨论】:

  • 什么是post_one_comment?还有为什么你在edit_comment中定义一个表单,然后马上换成不同的表单?
  • 现在将它添加到视图 - 部分下,用于在模板中发布视图 - 功能并保存表单。我不确定如何以另一种方式使用“实例”。
  • 但是你写的和foo = "bar"; foo = "baz"是一样的——第一个变量被定义然后被替换,foo就等于“baz”。首先将其定义为等于“bar”是没有意义的。
  • 现在我不明白postOneComment 的意义何在。您已经仔细定义了一个表单并使用实例对其进行了实例化,现在您已经定义了一个完全忽略该表单并直接从 POST 保存评论的单独函数。你为什么要这样做?
  • 好的,我不知道,如何使用实例变量更新表单字段?

标签: python django forms templates edit


【解决方案1】:

所以这里至少存在三个问题。

首先,正如我所说,您重复实例化 form 变量是没有意义的;就好像你从来没有做过第一个一样。所以不要。

其次,我不明白postOneComment 的意义何在。您已经仔细定义了一个表单并使用实例对其进行了实例化,现在您已经定义了一个完全忽略表单并直接从 POST 保存评论的单独函数。实际上,此代码实际上不起作用,因为您尝试将 username 字符串作为 user 参数而不是实际用户传递。

第三,您遇到的实际问题是您的表单无效。但是,您不会在模板上显示错误,因此您看不到它们 - 而且您也不会在成功发布后重定向到另一个 URL,因此您无法判断发布是否成功.

您的表单无效的原因是您在表单中明确包含user 参数,但随后没有在模板中提供它。因此,该表格将永远无效。显然你想自动设置它,所以从fields 中删除它。

所以,总结一下:

  • 从表单字段中删除user
  • {{ form.errors }} 添加到您的模板中;
  • 用以下代码替换您的视图代码:

.

def edit_comment(request, one_entry):
    content = Comment.objects.get(pk=one_entry)

    if request.method == 'POST':
        form = ReplyForm(request.POST, instance=content)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.user = request.user
            comment.save()
            return redirect('/')  # or wherever you want
    else:
        form = ReplyForm()
    c = {"form":form,"one_entry": content}
    return render(request,"template.html", c)

【讨论】:

  • 谢谢,它可以使用 this 而不是你所说的 post 函数。
猜你喜欢
  • 2020-07-25
  • 2018-12-03
  • 2014-08-11
  • 1970-01-01
  • 1970-01-01
  • 2016-10-23
  • 2013-04-12
  • 1970-01-01
  • 2015-02-24
相关资源
最近更新 更多