简体   繁体   中英

Django Form not saving to database?

I am using CreateView to create entries on my frontend and then save to the database/backend. But each time i click save or submit the form redirect successfully as expected but will not save to the back end.

I have tried adding success_url to my views.py and also added get_absolute_url(self) to my models.py but still it hasn't worked.

Views.py

class Dashboard (LoginRequiredMixin, CreateView): 
    model = PostedJob
    template_name ='accounts/dashboard.html'
    fields = ['firstname', 'lastname', 'job_title', 'email', 'budget', 
             'country', 'state', 'address', 'job_category', 
             'description', 'photo']
    success_message = "Redirect successfully created!"
    login_url= 'login'

models.py

Type = (
       ('building inspection', 'Building Inspection'),
       ('agriculture', 'Agriculture'),
       ('construction', 'Construction'),
       ('maintenance & training', 'Maintenance & Training'),
       ('surveying & mapping', 'Surveying & Mapping'),
       ('events coverage', 'Events Coverage'),
    )

 class PostedJob(models.Model):
  firstname=models.CharField(max_length=200)
  lastname=models.CharField(max_length =150)
  email=models.EmailField(max_length =150)
  job_title= models.CharField(max_length =150)
  budget=models.PositiveIntegerField()
  country=models.CharField(max_length = 150)
  state=models.CharField(max_length = 150)
  address=models.CharField(max_length = 150)
  job_category=models.CharField(choices=Type, default ='agriculture', 
  max_length=50 )
  photo= models.ImageField(upload_to='/%Y/%m/%d/', blank=False, 
  null=False)
  description=models.TextField(max_length = 1500)
  post_date=models.DateTimeField(default = datetime.now, blank=True)
  publish=models.BooleanField(default =False)

  def __str__(self):
    return self.job_title

  def get_absolute_url(self):
    return reverse('home') 

urls.py

  urlpatterns =[
    path('accounts/dashboard', Dashboard.as_view(), name='dashboard'),
 ]

index.html

<form action="" method="post">
  {% csrf_token %}
  {{form.as_p}}
   <div>
     <input type="submit" id="submit" value="Submit" class="btn waves- 
      effect waves-grey green">
   </div>
</form>

Also the form displays no error message. I will indeed be grateful for your help. Thanks.

The form shows no errors but using the following SQL queries in Python prompt 
revealed the error :

from django.db import connection
cursor = connection.cursor()

error: django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are 
not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call 
settings.configure() before accessing settings.

** Updated**

However the environment variable does not really solve this. The problem is actually from the form itself. The form has an image field which is required by default. The form even though it redirect successfully on click, did not save to the database. The reason being that since all fields are required, the image will returning a blank field on every save or submit action causing the form to constantly not save on every submit action.

Solution: Simply add enctype="multipart/form-data to your form. 

<form action="" method="POST" enctype="multipart/form-data">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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