1
如何在模板中設置who
和image
的值?Django如何設置模板中隱藏輸入的值
class CommentForm(ModelForm):
who = forms.CharField(widget=forms.HiddenInput())
image = forms.ImageField(widget=forms.HiddenInput())
class Meta:
model = Comments
fields = ['who', 'image', 'content']
它不工作(原始文本):
<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>
我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
你可以添加你的看法的問題? – wencakisa