提交的表单不保存在sql - dJango中

时间:2015-10-15 07:27:23

标签: python django

我搜索了一些有关此问题的帖子,但仍未找到解决方案。单击“提交”后,页面可以重定向到另一个页面。但是sql数据库没有显示提交的信息。

提前感谢您的建议。

views.py

@csrf_exempt
def input(request):
    if request.method == 'POST':
        form = InputForm(request.POST or None, request.FILES or None)
        if form.is_valid():
            company = form.cleaned_data['company']
            region = form.cleaned_data['region']
            uom= form.cleaned_data['uom']
            start_date= form.cleaned_data['start_date']
            end_date= form.cleaned_data['end_date']
            add_input=Input.objects.create(company=company,region=region,uom=uom,start_date=start_date,end_date=end_date)
            add_input.save()
            return redirect('resut')
        else:
            print(form.errors)
    else:
        form = InputForm(initial={'company':'coco','uom':'M$'},instance=Input)
        return render_to_response('inputform.html',{'form': form})

models.py

class Input(models.Model):
    company=models.CharField(max_length=100)
    region=models.CharField(max_length=100)
    uom=models.CharField(max_length=100)
    start_date=models.DateField(auto_now=False, auto_now_add=False)
    end_date=models.DateField(auto_now=False, auto_now_add=False)

forms.py

class InputForm(forms.ModelForm):
    company=forms.CharField(widget=forms.TextInput, label="Company",error_messages={'required': 'Please enter your name'},required=True)
    regionlist = forms.ModelChoiceField(queryset=Dupont.objects.values('region').distinct())
    uom=forms.CharField(required=True)
    start_date=forms.DateField(widget=DateInput(),required=True)
    end_date = forms.DateField(widget=DateInput(),required=True)
    error_css_class='error'
    required_css_class = 'required'

    class Meta:
        model = Input
        fields = ('company', 'region','uom','start_date','end_date')
        widgets = {
            'start_date': forms.DateInput(attrs={'class':'datepicker'}),
            'end_date': forms.DateInput(attrs={'class':'datepicker'}),
        }

html代码段

<form action="{% url 'result' %}" method="post">{% csrf_token %}

        <!--company--> 
        <div class="field">
            <p>Company:<input type="text" name="company" value="{{company}}"/>
        </div>


        <!--region-->
        <div class="field" >
            <label> Select the Region:
            {{ form.regionlist }}
                {% for region in form.regionlist.choices %}
                     <option value="{{ val }}" {% ifequal data.val val %}selected {% endifequal %}></option>
                {% endfor %}
            </label>
        </div>

        <!--uome-->
        <div class="field">
            <p>Unit of Measure:<input type="text" name="uom" value="{{uom}}"/>
        </div> 

        <!--start date-->
        <label for="startDate">Start Month:</label>
        <input name="start_date" id="startDate" class="date-picker"/>

        <!--END date-->
        <label for="endDate">End Month:</label>
        <input name="end_date" id="endDate" class="date-picker" />


        <!--submit-->
        <div class="fieldWrapper">
        <p><input type="submit" value="Submit" /></p></div>

{% if form.errors %}
    {% for field in form %}
        {% for error in field.errors %}
            <div class="alert alert-error">
                <strong>{{ error|escape }}</strong>
            </div>
        {% endfor %}
    {% endfor %}
    {% for error in form.non_field_errors %}
        <div class="alert alert-error">
            <strong>{{ error|escape }}</strong>
        </div>
    {% endfor %}
{% endif %}

  </form>

1 个答案:

答案 0 :(得分:3)

  

不,操作指向另一个视图,而不是输入视图。我没有粘贴该视图,因为现在问题是这个表单的数据没有成功保存。

那么那就是你的问题,当你提交表格时,它会转到指定的行动。如果代码段是inputform.html,我怀疑它就是您需要从表单中删除操作。

<form method="post">