Django 1.11'方法'对象不可订阅(表单/模型)

时间:2017-08-16 00:23:44

标签: django django-models django-forms django-views

我正在尝试创建一个上传字段,人们可以上传他们的简历/简历。当我点击“提交”按钮时,我收到以下错误:

    Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/src/project/views.py", line 160, in careersView
    applicant_cv = applyModel(cv = request.FILES.get['cv'])
TypeError: 'method' object is not subscriptable

我正在使用以下配置(Django 1.11,使用内置网络服务器进行测试,启用调试):

Models.py

from django.db import models

class applyModel(models.Model) :
    cv = models.FileField(upload_to='cvs/')

Forms.py

from django import forms
from django.forms import ModelForm
from .models import applyModel

class applyForm(forms.Form) :
    name = forms.CharField(label="", required=True, max_length=50, widget=forms.TextInput(attrs={'placeholder': 'Your Name', 'class': 'name'}))
    email = forms.EmailField(label="", required=True, max_length=100, widget=forms.EmailInput(attrs={'placeholder': 'E-mail Address', 'class': 'email'}))
    phone = forms.CharField(label="", required=False, max_length=20, widget=forms.NumberInput(attrs={'placeholder': 'Phone Number', 'class': 'phone'}))
    linkedin = forms.CharField(label="", required=True, max_length=100, widget=forms.TextInput(attrs={'placeholder': 'LinkedIn / Professional Website', 'class': 'linkedin'}))
    portfolio = forms.CharField(label="", required=False, max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Portfolio', 'class': 'portfolio'}))
    hours = forms.CharField(label="", required=True, max_length=10, widget=forms.NumberInput(attrs={'placeholder': 'Hours Available Per Week', 'class': 'hours'}))
    startDate = forms.CharField(label="", required=True, max_length=10, widget=forms.TextInput(attrs={'placeholder': 'Earliest Start Date', 'class': 'startDate'}))
    message = forms.CharField(label="", max_length=900, widget=forms.Textarea(attrs={'placeholder': 'Provide your motivation for this role (max five sentences)', 'class': 'message'}))
    position = forms.CharField(label="", max_length=900, widget=forms.Textarea(attrs={'placeholder': 'Message', 'class': 'position'}))
    cv = forms.FileField(label="Upload CV/Resume")

Views.py

from .forms import applyForm
from .models import applyModel


@csrf_exempt
def careersView(request):
    if request.method == "POST" :
        form = applyForm(request.POST, request.FILES)
        if form.is_valid():
            name = request.POST.get('name')
            email = request.POST.get('email')
            phone = request.POST.get("phone")
            linkedin = request.POST.get("linkedin")
            hours = request.POST.get("hours")
            startDate = request.POST.get("startDate")
            message = request.POST.get("message")
            position = request.POST.get("position")
            portfolio = request.POST.get("portfolio")
            applicant_cv = applyModel(cv = request.FILES.get['cv'])
            cv_filename = applyModel.objects.get()
            cv_filename = cv_filename.cv.name
            return render(request, '/careers/careers_template.html', {'form': form})
    else :
        form = applyForm()
    return render(request, '/careers/careers_template.html', {'form': form})

Settings.py

  I've added this template: 'django.template.context_processors.media',
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
    MEDIA_URL = '/media/'
    And yes, I have the following line in my urls.conf: 
    + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

HTML文件

   <form action="" method="post" enctype="multipart/form-data">
      <div class="closeBtn"><img src="{% static 'img/svgs/icon-close-sm-dark.svg'%}"/></div>
      <div class="icon">
        <img src=""/>
      </div>
      <h1>Application for: </br> </br> <span class="title"></span> <span class="hours"></span></h1>
      {{ form }}
      <input class="hvr-bob submitBtn" type="submit" name "contact" value="Send Message" />
      <div class="success">
        <p><svg class="checkmark" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 52 52"><circle class="checkmark__circle" cx="26" cy="26" r="25" fill="none"/><path class="checkmark__check" fill="none" d="M14.1 27.2l7.1 7.2 16.7-16.8"/></svg> </br> <span>Thank you for applying!</span>  </br> We will be in touch shortly.</p>
      </div>
    </form>

1 个答案:

答案 0 :(得分:2)

您使用方括号调用方法get,请尝试以下方法:

applicant_cv = applyModel(cv = request.FILES.get('cv'))