[英]How can I change the default Django Form name?
為了更好地了解我正在加載哪個表單的頁面,我想更改在模板中訪問它的名稱。 據我所知,默認情況下它設置為“表單”。 在下面的片段中,我嘗試使用“form_inv”加載表單,但這不起作用。
{% extends "dashboard/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Create Investment</legend>
{{ form_inv|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Create</button>
</div>
</form>
</div>
{% endblock content %}
下面是我創建的Model和各自的視圖。
Model:
class MoneyGroups(models.Model):
title = models.CharField(max_length=100)
size = models.IntegerField()
interest = models.FloatField()
Django 查看:
class MoneyCreateView(LoginRequiredMixin, CreateView):
model = MoneyGroups
template_name = 'dashboard/scenario_form.html' # <app>/<model>_<viewtype>.html
context_object_name = 'investments'
fields = ['title', 'size', 'interest']
success_url = '/dashboard/'
def form_valid(self, form):
form.instance.author = self.request.user #add the author to the form before it is submitted
return super().form_valid(form)
我必須在 model 或視圖中更改/添加什么參數才能更改我可以在模板中訪問它的名稱?
謝謝你的幫助!
不知道為什么要這樣做,但將 get_context_data 方法添加到您的視圖中,如下所示:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context[‘form_inv’] = context[‘form’]
return context
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.