Sunday 15 January 2012

python - Simple file upload page not working -


I am trying to add a simple file upload page to my site. Whenever I submit the form, it says "This field is required" above the file selector, as if it is not crossing it. Here's my form, model, and footage.

forms.py

  django.http imports from Django import forms from HttpResponseRedirect django.shortcuts render_to_response class UploadFileForm (forms .Form): MURRMACMOD = "MMM" CUBE67 = "C67" CUBE20 = "C20" VDINCONN = "VDC" IMPACT20 = "I -20" IMPACT67 = "I67" CAT_CHOICES = ((MURRMACMOD, 'Murrelektronik MAC modules'), (CUBE67, 'Cube67 / Cube67') , (CUBE20, 'Cube20'), (VDINCONN, 'valve DIN connectors'), (IMPACT20, 'impact 20'), (IMPACT67, 'impact 67')) article = forms.CharField (MAX_LENGTH = 100) title = forms .CharField (MAX_LENGTH = 100) file = forms.FileField () cat = forms.ChoiceField (options = CAT_CHOICES)   

models.py

  django Db import model class file (models.mo Del) of article = models.CharField (MAX_LENGTH = 100) title = models.CharField (MAX_LENGTH = 100) file = models.FileField () cat = models.CharField ()   

views .py

  from django.http Import django.shortcut the httpResponseRedirect S import render_to_response import.text_to_response files.forms importPonteForm def upload_file (request): If request.method == ' POST ': form = UploadFileForm (request.POST, request.FILES) If form.is_valid (): Form.save () returns HttpResponseRedirect (' / success / url / ') other: form = UploadFileForm () ctxt_dict = {' form ': Form,} context = RequestContext (request, ctxt_dict) return render_to_response (' upload.html ', context_instance = context)   

I have some help friends, thank you Please sign

Edit: .. Here's the template

  {% if user.is_staff%} & lt; Form action = "." Method = "post" & gt; {% Csrf_token%} {{form.as_p}} & lt; Input type = "submit" value = "submit" /> {% Else%} {% endif%} & lt; / Form & gt; If you are trying to render the form to the file format, then in the DJ ango template    

< P>

form method = "POST" enctype = "with multipart / form

form method =" post "action =" "& gt;

Must change. -Data "& gt;



No comments:

Post a Comment