django UpdateView widget_tweaks'str'对象没有属性'as_widget'

时间:2018-11-03 11:08:30

标签: python django

我是django的新手,正在开发一个小型应用程序,其中必须更新项目模型。我正在使用django内置的UpdateView更新已保存项目的详细信息。我还使用widget_tweaks渲染具有自定义引导程序类的字段以进行样式设置。当我运行此代码时,它显示 str对象没有属性'as_widget 错误。我认为表单字段以字符串形式而不是对象形式进入模板,并且 widget tweak 的render_field标签无法呈现它,但是我不知道为什么它以字符串形式出现? 。我只想更新模型的某些字段。这就是为什么我在UpdateView中使用“字段”的原因。但是我也尝试了form_class只是为了检查,但是那也不起作用。我在这个错误上停留了两天。任何帮助将不胜感激。

以下是项目模型

class ProjectUpdateView(UpdateView):
    model = Project
    fields = ('hubstaffName','endDate', 'hoursToWork' ,'paymentType','ProjectPaymentAmount','sourceOfProject','sourceName',)
    #form_class = ProjectForm
    template_name = "Administration/Manage-Projects.html"
    context_object_name = 'project_form' #name of the context object in the template
    pk_url_kwarg = 'pid'

这是视图

path('edit-Project/<int:pid>',ProjectUpdateView.as_view(),name='editProject')

以下是网址部分。

<form action="{% url 'Administration:editProject' pid %}" method="post">
                        {% csrf_token %}
                        <button id="EditProject" type="submit"  class="btn btn-primary mx-3 my-2" >Edit this Project</button>
                    </form>

在此按钮上单击它,将其发布到我正在使用更新视图的“ Administration:editProject”视图中。

{% extends 'base.html' %}
{% load widget_tweaks %}
{% block content %}



    <div class="container py-3">
    <div class="row">
        <div class="mx-auto col-sm-6">
                    <!-- form user info -->
                    <div class="card">
                        <div class="card-header">
                            <h4 class="mb-0">Add Project</h4>
                        </div>
                        <div class="card-body">

                            <form method="post" class="form" role="form">
                                {% csrf_token %}
                                <div class="form-group row">
                                    <label class="col-lg-3 col-form-label form-control-label">{{ project_form.name.label }}</label>
                                    <div class="col-lg-9">
                                        {% render_field project_form.name class="form-control" %}
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label class="col-lg-3 col-form-label form-control-label">{{ project_form.startDate.label }}</label>
                                    <div class="input-group date col-lg-9" data-provide="datepicker">
                                        {% render_field project_form.startDate class="form-control" %}
                                        <div class="input-group-addon">
                                            <span class="glyphicon glyphicon-th"></span>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label class="col-lg-3 col-form-label form-control-label">{{ project_form.endDate.label }}</label>
                                    <div class="input-group date col-lg-9" data-provide="datepicker">
                                        {% render_field project_form.endDate class="form-control" %}
                                        <div class="input-group-addon">
                                            <span class="glyphicon glyphicon-th"></span>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label class="col-lg-3 col-form-label form-control-label">{{ project_form.sourceOfProject.label }}</label>
                                    <div class="col-lg-9">
                                        {% render_field project_form.sourceOfProject class="form-control" %}
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label class="col-lg-3 col-form-label form-control-label">{{ project_form.sourceName.label }}</label>
                                    <div class="col-lg-9">
                                        {% render_field project_form.sourceName class="form-control" %}
                                    </div>
                                </div>

                                <div class="form-group row">
                                    <label class="col-lg-3 col-form-label form-control-label">{{ project_form.paymentType.label }}</label>
                                    <div class="col-lg-9">
                                        {% render_field project_form.paymentType class="form-control" %}
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label class="col-lg-3 col-form-label form-control-label">{{ project_form.ProjectPaymentAmount.label }}</label>
                                    <div class="col-lg-9">
                                        {% render_field project_form.ProjectPaymentAmount class="form-control" %}
                                    </div>
                                    </div>
                                    <div>


                                    </div>

                                        <div>

                                            </div>




                                        <div>
                                            <button class="btn btn-primary" name="save_home" type="submit">Save and Return</button>
                                            <button class="btn btn-primary" name="save_another" type="submit">Save and Add Another</button>
                                        </div>
                            </form>

        </div>
    </div>
</div>
    </div>
    </div>

以下是我用来编辑Project的编辑模板。

class ProjectForm(forms.ModelForm):
        class Meta:
            model = Project
            fields = ('name' , 'startDate' ,'endDate' , 'sourceOfProject','sourceName','paymentType','ProjectPaymentAmount','hoursToWork' )
            widgets = {'startDate':forms.SelectDateWidget(), 'endDate':forms.SelectDateWidget(),  'sourceOfProject':forms.Select()}

这里是ModelForm

{{1}}

1 个答案:

答案 0 :(得分:0)

您应该在form_class = ProjectForm中定义UpdateView,其中ProjectFormforms.py模型中应在Project中定义。比起如下所示,您可以在模板中呈现字段。

Manage-Projects.html

{% for field in form %}
    <div class="form-group">
        {% render_field field class="form-control" %}
    </div>
{% endfor %}

views.py

class ProjectUpdateView(UpdateView):
    model = Project
    form_class = ProjectForm
    template_name = "Administration/Manage-Projects.html"
    context_object_name = "project"

forms.py

class ProjectForm(forms.ModelForm):
        class Meta:
            model = Project
            fields = ('name' , 'startDate' ,'endDate' , 'sourceOfProject','sourceName','paymentType','ProjectPaymentAmount','hoursToWork' )
            widgets = {'startDate':forms.SelectDateWidget(), 'endDate':forms.SelectDateWidget(),  'sourceOfProject':forms.Select()}

问题也可能出在您作为表单实现的“编辑”按钮中。像显示的波纹管一样使用锚标记,并将project.pk发送到网址。

<a class="btn btn-primary" href="{% url 'Administration:editProject' project.pk %}">Edit</a>

然后您可以在UpdateView中删除pk_url_kwargs并更新URL。

path('edit-Project/<int:pk>',ProjectUpdateView.as_view(),name='editProject')