Jump to content

Recommended Posts

Posted

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • 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.