简体   繁体   English

Django 表单未保存到数据库

[英]Django Form is Not saving to Database

I have searched the site for a solution, many related questions, but no direct response given.我已经在该网站上搜索了解决方案,许多相关问题,但没有给出直接回复。 I have 3 apps on my projects, and 2 worked pretty well, except for this app, which is not writing to the database.我的项目中有 3 个应用程序,其中 2 个运行良好,除了这个应用程序没有写入数据库。

Yes, the other 2 apps write to the postgres database, and I can view the table, but this return empty rows, and I don't see any problem.是的,其他 2 个应用程序写入 postgres 数据库,我可以查看表,但这会返回空行,我没有看到任何问题。 I hope someone can help me, see below my Model, Form and View.py.希望有人能帮帮我,看下面我的Model,Form and View.py。

MY View.py我的视图.py

from django.shortcuts import render, redirect
from .forms import EngForm

def EngineerList(request):
    return render(request, "Engineers/elist.html")


def EngineerForm(request):
    if request.method == "GET":
         form = EngForm()
         return render(request, "Engineers/eform.html", {'form':form})
    else:
         form = EngForm(request.POST)
         if form.is_valid():
            form.save()
         return redirect('/engineer/') #pointing to urls.py paths

My Forms.py我的 Forms.py

from django import forms
from .models import Engineers

class EngForm(forms.ModelForm):

      class Meta:
          model = Engineers
          fields = '__all__'
          labels = {
             'workarea' : 'Zone',
             'face' : 'Picture',
    
            }

      def __init__(self, *args, **kwargs):
          super(EngForm, self).__init__(*args, **kwargs)
          self.fields['position'].empty_label='Select'
          self.fields['workarea'].empty_label='Select'
          self.fields['face'].required = False

My Model.py我的 Model.py

from django.db import models

class Engineers(models.Model):
   position = (
       ('NOC', 'NOC'),
       ('Supervisor', 'Supervisor'), 
       ('Site Manager','Site Manager'), 
       ('Site Engineer', 'Site Engineer'),
      )

   region = (
       ('SS','South-South'),('SW','SW'),('SE','SE'), 
       ('NE','NE'),('NW','NW'),('NC','NC'),
       )

   firstname = models.CharField(max_length=20)
   lastname = models.CharField(max_length=20)
   email = models.EmailField(max_length=50)
   username = models.CharField(max_length=20)
   phone = models.IntegerField()
   position = models.CharField(max_length=20, choices=position)
   workarea = models.CharField(max_length=20, choices=region)
   face = models.ImageField(upload_to='', height_field=15, width_field=9)

My HTML我的 HTML

{% extends "Dashboard/base.html" %}
{% load crispy_forms_tags %}


{% block content %}
<form method="POST" action="" autocomplete="off">
{% csrf_token %}
<div class="row">
    <div class="col-md-12">
        {{form.face|as_crispy_field}}
    </div>
    <p>
    <hr>
    <p>
    <div class="col-md-6">
        {{form.firstname|as_crispy_field}}
    </div>
    <div class="col-md-6">
        {{form.lastname|as_crispy_field}}
    </div>
     <div class="col-md-6">
        {{form.username|as_crispy_field}}
    </div>
    <div class="col-md-6">
        {{form.position|as_crispy_field}}
    </div>
     <div class="col-md-6">
        {{form.phone|as_crispy_field}}
    </div>
    <div class="col-md-6">
        {{form.workarea|as_crispy_field}}
    </div>
     <div class="col-md-12">
        {{form.email|as_crispy_field}}
    </div>
    
  </div>
  <hr>   
  <div>
      <button type="submit" class="btn btn-success"><i class="fas fa-save"></i> Submit</button>
  </div>
 </form>
 {% endblock content %}

My problem is, the database table is returning empty row.我的问题是,数据库表返回空行。

Solved.解决了。 Applied @GwynBleidD suggest, I reviewed the doc, and then rearranged the code as such.应用@GwynBleidD 建议,我查看了文档,然后重新排列了代码。

def EngineerForm(request):
if request.method == "POST":
    form = EngForm(request.POST)
    if form.is_valid():
        form.save()
        return redirect('/engineer/') #pointing to urls.py paths
else:
    form = EngForm()
return render(request, "Engineers/eform.html", {'form':form})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM