0

在我的 htmlpage 中,我有 100 个字段
,我有一个用于上传文件的字段,这对用户来说是可选的。
当我提交带有所选文件且其他几个字段留空的表单时,它已保存到数据库中。

同时,当我尝试在不选择要上传的文件的情况下提交表单时,它会引发错误,因为“”Key 'up_file' not found in “”

--models.py--

class js_details(models.Model):
    user = models.ForeignKey(User, unique=True)
    fname = models.CharField(max_length=50)
    lastname = models.CharField(max_length=50)
    dob = models.CharField(max_length=20, null=True)
    sec_email = models.EmailField(max_length=50, null=True)
    address1 = models.CharField(max_length=50, null=True)
    address2 = models.CharField(max_length=50, null=True)
    up_file = models.FileField(upload_to='documents', null=True)

--views.py--

def newpost(request):
if request.method == "POST":
        user_id = request.POST.get('user_id')

        fname= request.POST.get('fname')
        lastname= request.POST.get('lastname')
        dob = request.POST.get('dob')
        sec_email = request.POST.get('sec_email')
        address1 = request.POST.get('address1')
        address2 = request.POST.get('address2')
        up_file = request.FILES['up_file']
       p = js_details(user_id=user_id,fname=fname,lastname=lastname,dob=dob,sec_email=sec_email,address1=address1,address2=address2,up-file=up_file)
       p.save()

如何在不填写文件字段的情况下保存表单。?

欢迎您的所有回答。提前致谢。

4

1 回答 1

0

严格回答您的问题:您在 request.FILES (=> KeyError request.FILES.get('up_file') None mapping dict` 上使用下标访问以获取request.FILES['up_file']), which indeed raises a有关if there's no matchinh key. You should use这些and check the returned value which will be数据if the user didn't post a file. Read the Python's doc about类型and的更多信息。

现在您的代码还有另一个问题。第一个是您盲目地接受任何用户输入,而无需验证、清理和数据类型转换。使用 aModelForm可以解决这个问题并简化您的代码。

另外,作为旁注,您应该真正坚持 Django / Python 命名约定并使用 CapCase 作为您的类名。

这是您的代码的固定版本:

# models.py 
class JsDetails(models.Model):
    # ForeignKey -> OneToOneField, 
    # cf https://docs.djangoproject.com/en/1.4/ref/models/fields/#onetoonefield
    user = models.OneToOneField(User, unique=True)
    fname = models.CharField(max_length=50)
    lastname = models.CharField(max_length=50)
    # I assume "dob" means "date of birth"
    # so CharField -> DateField
    dob = models.DateField(blank=True, null=True)
    sec_email = models.EmailField(max_length=50, blank=True, null=True)
    address1 = models.CharField(max_length=50, blank=True, null=True)
    address2 = models.CharField(max_length=50, blank=True, null=True)
    up_file = models.FileField(upload_to='documents', blank=True, null=True)

# forms.py
from .models import JsDetail
from django import forms

class JsDetailForm(forms.ModelForm):
    class Meta:
        models = JsDetail

# views.py
from .forms import JsDetailForm 
from django.shortcuts import redirect, render

def newpost(request):
    if request.method == "POST":
        form = JsDetailForm(request.POST, request.FILES)
        if form.is_valid():
            newdetail = form.save()
            redirect("/somewhere/to/show/the/result")
        # if the form is not valid it will be redisplayed
        # to the user with error messages

    else:
        form = JsDetailForm()

    return render(
        request, 
        "your/template.htm",
        dict(form=form)
        ) 

恕我直言,此代码仍然存在问题,但这仍然是一个改进。我强烈建议你花点时间学习 Python 教程和 Django 教程,它会为你省去很多痛苦和麻烦。

于 2013-04-19T08:53:12.143 回答