Django如何在模板中设置隐藏输入的值

时间:2022-06-04 00:00:08

How to set value of who and image in template?

如何在模板中设置who和image的值?

class CommentForm(ModelForm):
    who = forms.CharField(widget=forms.HiddenInput())
    image = forms.ImageField(widget=forms.HiddenInput())

    class Meta:
        model = Comments
        fields = ['who', 'image', 'content']

It doesn't work (raw text):

它不工作(原始文本):

<form method='POST' action=''>
    {% csrf_token %}
    {% render_field comment_form.content class="form-control form-control-sm" placeholder='Comment..' %}
    {% render_field comment_form.who class="form-control form-control-sm" value='{{ request.user.profile.pk }}' %}
    {% render_field comment_form.image class="form-control form-control-sm" value='{{ image.pk }}' %}
    <input class="btn btn-primary btn-sm" type="submit" value="Add comment">
</form>

My views.py:

我的views.py:

class ProfileView(DetailView):
    comment_form = CommentForm()
    queryset = Profile.objects.all()
    context_object_name = 'me'
    template_name = 'profile.html'

    def get_context_data(self, **kwargs):
        context = super(ProfileView, self).get_context_data(**kwargs)
        context['comment_form'] = self.comment_form
        return context

2 个解决方案

#1


1  

You need to set the initial property of the form field, after you've instantiated the form in your view. Like so:

在实例化了视图中的表单之后,需要设置表单字段的初始属性。像这样:

class ProfileView(DetailView):
    comment_form = CommentForm()
    queryset = Profile.objects.all()
    context_object_name = 'me'
    template_name = 'profile.html'

    def get_context_data(self, **kwargs):
        context = super(ProfileView, self).get_context_data(**kwargs)
        context['comment_form'] = self.comment_form
        # This sets the initial value for the field:
        context['comment_form'].fields['who'].initial = self.request.user.profile.pk
        return context

#2


0  

<form method='POST' action=''>
    {% csrf_token %}
    {% render_field comment_form.content class="form-control form-control-sm" placeholder='Comment..' %}
    {% render_field comment_form.who class="form-control form-control-sm" value='{{ omment_form.who }}' %}
    {% render_field comment_form.image class="form-control form-control-sm" value='{{ comment_form.image }}' %}
    <input class="btn btn-primary btn-sm" type="submit" value="Add comment">
</form>

#1


1  

You need to set the initial property of the form field, after you've instantiated the form in your view. Like so:

在实例化了视图中的表单之后,需要设置表单字段的初始属性。像这样:

class ProfileView(DetailView):
    comment_form = CommentForm()
    queryset = Profile.objects.all()
    context_object_name = 'me'
    template_name = 'profile.html'

    def get_context_data(self, **kwargs):
        context = super(ProfileView, self).get_context_data(**kwargs)
        context['comment_form'] = self.comment_form
        # This sets the initial value for the field:
        context['comment_form'].fields['who'].initial = self.request.user.profile.pk
        return context

#2


0  

<form method='POST' action=''>
    {% csrf_token %}
    {% render_field comment_form.content class="form-control form-control-sm" placeholder='Comment..' %}
    {% render_field comment_form.who class="form-control form-control-sm" value='{{ omment_form.who }}' %}
    {% render_field comment_form.image class="form-control form-control-sm" value='{{ comment_form.image }}' %}
    <input class="btn btn-primary btn-sm" type="submit" value="Add comment">
</form>