2013-04-17 19 views
0

在我的html頁面中,我有100個字段
我有一個上傳文件的字段,對於用戶是可選的。
當我提交的表格與選擇的文件和其他幾個字段留空白, 它被保存到數據庫。django在MultiValueDict中找不到Key'up_file'{}「

在當我嘗試提交表單,而不選擇 文件同時上傳,它是提高誤差「「

--models」「up_file」未發現 密鑰」。 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() 

如何在不填充文件字段的情況下保存表單。

歡迎大家回答。 在此先感謝。

+0

爲什麼你使用'request.POST.get'作爲POST數據而不是'request.FILES'?看起來你是偶然編程... 我強烈建議你花一些時間學習Python和Django(提示:都有很好的教程和相當廣泛的文檔)。 –

+0

我對python和django非常陌生,並且在學習我自己,基本上我是從核心方面開始的。所以不要把我誤解爲我的愚蠢錯誤。 – USER

回答

0

要嚴格回答你的問題:您正在使用request.FILES標訪問(=>request.FILES['up_file']), which indeed raises a KeyError異常if there's no matchinh key. You should use request.FILES.get( 'up_file')and check the returned value which will beif the user didn't post a file. Read the Python's doc about映射and dict`更多關於這些數據類型。

現在有你的代碼中的另一個問題。第一個是,你盲目地接受未經驗證,消毒和數據類型轉換的任何用戶輸入。使用ModelForm將照顧這並簡化你的代碼。

而且作爲附註,你應該堅持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教程,它會爲你節省很多痛苦和麻煩。