Django的通用更新视图:如何将值传递到隐藏的表单域?

idv4meu8  于 2023-01-03  发布在  Go
关注(0)|答案(1)|浏览(128)

我正在尝试将Django的通用UpdateView和隐藏输入字段结合起来。我的目的是跟踪帖子在创建后是否被编辑过。隐藏字段的名称是"updated"。
views.py:

class PostUpdateView(UpdateView):
  model = Post
  template_name = 'journal/post_update_form.html'
  form_class = PostUpdateForm
  success_url = reverse_lazy('my_posts')

models.py:

class Post(models.Model):
  title = models.CharField(max_length=500)
  text = models.TextField(max_length=2000, null=True, blank=True)
  owner = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
  #storing the link to media files in CharField
  image = models.CharField(max_length=500, null=True, blank=True)
  audio = models.CharField(max_length=500, null=True, blank=True)
  video = models.CharField(max_length=500, null=True, blank=True)
  rubric = models.CharField(max_length=100, default="No rubric", choices=RUBRIC_CHOICES)
  private = models.BooleanField(default=False)
  created_at = models.DateTimeField(auto_now_add=True)
  updated_at = models.DateTimeField(auto_now=True)
  updated = models.CharField(max_length=10, default="False")

forms.py:

from django import forms
from .models import Post

class PostUpdateForm(forms.ModelForm):
  class Meta:
    model = Post
    fields = ["title", "text", "image", "audio", "video", "rubric", "private", "updated"]
    widgets = {'updated': forms.HiddenInput(attrs={'value':"updated"})}

更新表格模板相关部分:

<form action="" method="POST">
        {% csrf_token %}

        {% for field in form %}
          {% if field != form.rubric and field != form.private %}
            <div class="mb-3 form-group">
              <label for="{{field.name}}" class="form-label">{{field.label}}</label>
                {{field.errors}}
                {% if field == form.text %}
                  <textarea type="text" class="form-control" id="{{field.name}}" name="{{field.name}}">{{field.value|default_if_none:"" }}</textarea>
                {% elif field == form.updated %}
                  <input type="hidden" id="{{field.name}}" name="{{field.name}}" value="updated">
                {% else %}
                  <input type="text" class="form-control" id="{{field.name}}" name="{{field.name}}" value="{{field.value|default_if_none:"" }}">
                {% endif %}
            </div>

           <...some other fields...>
            {% endif %}
        {% endfor %}

        <input type="submit" class="btn btn-primary" value="Update"/>
      </form>

Value of "updated" field is passed successfully, but the field value is visible (non-editable). In forms.py I tried to:

  • 从字段列表中排除"updated"。结果:不保存"更新"的值。
  • 使用"小部件={'更新':表单.隐藏输入} "。结果:值也成功传递,但字段仍然可见。
  • use only widgets in forms.py and comment out input field [1] in the template. Result: field is visible, editable, value is not saved.

[1]输入字段:

{% elif field == form.updated %}
                  <input type="hidden" id="{{field.name}}" name="{{field.name}}" value="updated">

我将非常感谢任何关于这里可能出了什么问题的见解。提前谢谢你。

zbdgwd5y

zbdgwd5y1#

在PostUpdateForm中,您可以执行以下操作:

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)

    self.fields['updated'].initial = 'updated'
    self.fields['updated'].disabled = True

https://docs.djangoproject.com/en/4.1/ref/forms/fields/#disabled

相关问题