python - 基于 Django 类的 View : Override form name

标签 python django django-class-based-views django-generic-views

我是 Django 的新手。我尝试构建基于类的 View 以创建对象。

模板中表单的默认名称是 form,我想将其更改为 "ajoutersource" 但我不知道如何更改。

views.py

class ajoutSource(CreateView):
    model = Source
    template_name = "pmd/ajouterSource.html"
    form_class = AjouterSourceForm
    success_url = reverse_lazy(ListerSources)

ajouterSource.html

{% for field in ajoutersource %} 
    <div class="row"> 
        {% if field.errors %}
            <div class="error">{{ field.errors }}</div> 
        {% endif %}
        <div class="label">{{ field.label }}</div> 
        <div class="field">{{ field }}</div>
    </div> 
{% endfor %}

最佳答案

覆盖 get_context_data() :

class ajoutSource(CreateView):
    model = Source
    template_name = "pmd/ajouterSource.html"
    form_class = AjouterSourceForm
    success_url = reverse_lazy(ListerSources)

    def get_context_data(self, **kwargs):
        context = super(ajoutSource, self).get_context_data(**kwargs)
        context["ajoutersource"]=context["form"]
        return context

关于python - 基于 Django 类的 View : Override form name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33485805/

相关文章:

django - 在 django View 中,如果用户不属于对象集,我如何将用户重定向到登录 View ?

python - R 在 python 中具有函数

python - 使用 django 进行 mp3 流传输的问题

python - Django - get_by_natural_key() 恰好接受 3 个参数(给定 2 个)

python - 如何在django-sphinx下自动重建Sphinx索引?

django - Getting __init__() got an unexpected keyword argument 'instance' with CreateView of Django

python - 在 Django 中运行纯 SQL 查询时如何获取字段名称

python - OpenCV - 绘制线条或矩形时出错

python - 是否可以在设备上加载 .gpx 轨道以使用 appium 测试跟踪器?

python - 基于 Django 类的 View 获取发布数据并发送响应