Helio13 Posted April 19, 2013 at 01:25 PM Report #503991 Posted April 19, 2013 at 01:25 PM Estou a tentar fazer um upload de imagem em Django, mas não estou a conseguir... Estou a tentar fazer da seguinte maneira: Models.py: class TestUpload(models.Model): model_pic = models.ImageField(upload_to='Temp/', blank=True, null=True) Forms.py: class TestUploadForm(forms.Form): image = forms.ImageField() Views.py: def index(request): if request.method == 'POST': form = TestUploadForm(request.POST, request.FILES) if form.is_valid(): form.save() return render_to_response('index.html', {'form': form}) else: form = TestUploadForm() return render_to_response('index.html', {'form': form}, context_instance=RequestContext(request) ) index.html: <!doctype html> <html> <head> <title>{% block title %}{% endblock %}</title> </head> <body> <marquee>Teste!</marquee> {% block content %}{% endblock %} <h1>Inserir</h1> <form action="" method="post" enctype="multipart/form-data">{% csrf_token %} <p> <input id="id_image" type="file" class="" name="image"> </p> <input type="submit" value="Submit" id="Save" /> </form> </body> </html> Selecciono a imagem, depois quando carrego no botão "Submit", diz me: 'TestUploadForm' object has no attribute 'save' Alguém me pode ajudar? Sou novo em Django... Obrigado
xventil Posted April 22, 2013 at 08:40 AM Report #504264 Posted April 22, 2013 at 08:40 AM Boas, não sou muito experiente no assunto, mas creio que tens de criar um ModelForm... tenta ver estes links que te deixo aqui.... https://docs.djangoproject.com/en/dev/topics/forms/modelforms/
Helio13 Posted April 22, 2013 at 09:23 AM Author Report #504274 Posted April 22, 2013 at 09:23 AM Modifiquei o meu código, e agora encontra-se desta forma: (Está no link abaixo) http://plnkr.co/edit/neqmvI13prMySMtHQHta Mas continua a não guardar a imagem... Não devia estar a guardar a imagem na pasta?
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now