即使引发异常,条目也会进入数据库

时间:2019-06-30 15:33:21

标签: python django

所以我有一个需要一些输入和一个文件字段的表格。我正在对文件归档使用验证器,以检查文件的大小。之所以运作良好,是因为我在页面上收到错误消息,指出文件太大,但条目仍进入DB。

我不知道为什么会这样。我以为引发异常可以解决我的问题,但事实并非如此。 models.py

from django.db import models
from django.contrib.auth.models import User
from .validators import validate_file_size

# Create your models here.

class CV(models.Model):
    solicitant = models.ForeignKey(User, on_delete=models.CASCADE)
    dataUploadCV = models.DateField(auto_now_add=True)
    nume = models.CharField(max_length=12)
    prenume = models.CharField(max_length=12)
    telefon = models.CharField(max_length=12)
    emailContact = models.EmailField(max_length=40)
    CV = models.FileField(upload_to='documents/%d/%m/%Y', validators=[validate_file_size])
    rezolvata = models.BooleanField(default=False)
    def __str__(self):
        return self.nume + " " + self.prenume + ": " + str(self.CV)

validators.py

from django.core.exceptions import ValidationError


def validate_file_size(value):
    filesize=value.size

    if filesize > 1:
         raise ValidationError("Fisierul poate avea maxim 5MB.")
    else:
        return value

views.py

from django.shortcuts import render, get_object_or_404
from .models import Oferta, CV
from django.contrib import messages
from django.core.paginator import Paginator

# Create your views here

def incarcarecv(req):
    context = {
        'title': "Incarcare CV | Best DAVNIC73"
    }
    if req.method == 'POST':
        try:
            nume = req.POST['nume']
            prenume = req.POST['prenume']
            telefon = req.POST['telefon']
            email = req.POST['email']
            cv = req.FILES['CV']
            try:
                if(req.user.is_authenticated):
                    cv_upload = CV(
                    solicitant=req.user,
                    nume=nume,
                    prenume=prenume,
                    telefon=telefon,
                    emailContact=email
                    )
                    cv_upload.CV.save(cv.name, cv)
                    cv_upload.full_clean()
                    cv_upload.save()
                    req.user.profile.cvuri.append(cv_upload.id)
                    req.user.profile.save()
                    messages.success(req, 'CV depus cu succes!') 
                else:
                    messages.error(req, 'Trebuie sa fii logat pentru a depune CV-ul!')
            except (ValueError):
                messages.error(req, 'Formularul nu a fost incarcat!')
                messages.info(req, 'Verifica daca esti logat!')
        except:
            messages.error(req, 'Nu ai completat corect campurile sau unul din ele este liber!')
            messages.info(req, 'Ai grija ca toate campurile sa fie completate si ca fisierul sa nu depaseasca 5MB!')
    return render(req, "../templates/pagini/incarcare-cv.html", context)

html文件

{% extends 'base.html' %}
{% load static %}
{% block content %}
            <div class="container container-centru">
                <h1 class="heading-contact">Incarca CV</h1>
                {% include 'partials/_alerts.html' %}
                <form action="{% url 'incarcarecv' %}" method="POST" class="form-contact"  enctype="multipart/form-data">
                    {% csrf_token %}
                        <div class="form-group">
                            <label for="inputnume" class="email-contact">Nume</label>
                            <input type="text" name="nume" class="form-control" id="inputnume" aria-describedby="numeHelp" placeholder="Introdu nume">
                        </div>
                        <div class="form-group">
                                <label for="inputprenume" class="email-contact">Prenume</label>
                                <input type="text" name="prenume" class="form-control" id="inputprenume" aria-describedby="prenumeHelp" placeholder="Introdu prenume">
                        </div>
                        <div class="form-group">
                            <label for="inputtelefon" class="email-contact">Telefon</label>
                            <input type="text" name="telefon" class="form-control" id="inputtelefon" aria-describedby="telefonHelp" placeholder="Introdu telefon">
                        </div>
                        <div class="form-group">
                            <label for="inputemail" class="email-contact">Email</label>
                            <input type="email" name="email" class="form-control" id="inputemail" aria-describedby="emailHelp" placeholder="Introdu email">
                        </div>
                        <div class="form-group">
                                <label for="inputcv" class="email-contact">CV</label>
                                <input type="file" name="CV" accept=".docx,.doc,.pdf,application/msword" class="form-control" id="inputemail" aria-describedby="CVHelp">
                            </div>
                        <div class="form-group form-group-custom">
                                <input type="submit" value="Trimite" class="btn btn-secondary btn-block btn-login-custom">
                                <input type="submit" value="Resetează câmpurile" class="btn btn-secondary btn-block btn-reset-custom">
                        </div>                   
                </form>
            </div>
            <script src="{% static 'javascript/clearMessage.js' %}"></script>                  
{% endblock %}

因此,如果引发异常,如何使帖子不进入数据库?现在,我得到了错误,但它仍在进入数据库。 (我可以在django的管理区域中看到它)

///// edit:我从代码中删除了所有与我的问题无关的内容。 //// edit2:添加了HTML文件

1 个答案:

答案 0 :(得分:1)

在Django中,典型的工作流程是首先运行验证程序以验证您尝试提交到数据库中的数据是否“干净”,然后才应调用save()。但是由于某些怪癖,调用save不会自动清除数据。

因此工作流程应类似于以下内容:

...
cv_upload = CV(solicitant=req.user, nume=nume, prenume=prenume, 
               telefon=telefon, emailContact=email)
# call clean before saving as we only want CLEAN data in the DB 
cv_upload.full_clean()
# now that it is relatively clean with Django's validators and our custom validators
# go ahead and save it
cv_upload.save()
...