Jump to content
Sign in to follow this  
Helio13

Django - Upload de imagem

Recommended Posts

Helio13

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.