0
我想創建一個上傳字段,人們可以上傳他們的簡歷/簡歷。當我點擊「提交」按鈕,我得到以下錯誤:Django 1.11'方法'對象不是可以下載的(Form/Model)
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,與內置的Web服務器,啓用調試測試):
模型的.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>
我沒有收到錯誤了,但該文件沒有保存在我的媒體目錄中。當我按提交按鈕時,我得到一個HTTP 200,並沒有錯誤。 –
@ErikN - 你解決了嗎?我有同樣的問題 – ggdx
嗨@DanWhite,是的,我能解決它,我知道這可能聽起來有點奇怪,但它與custom.js文件有關。在我從表單頁面刪除了這個文件(包含)之後,它就工作了。也許你可以刪除一些JS文件,然後嘗試它是否工作(custom.js顯然是每個人都不同)。 –