1
I’m trying to make a simple app: I made the tutorial of Django Girls and now I’m trying to use the same including also an image using the Cloudinary
But I’m having the following mistake:
Error happens on line upload_result = cloudinary.uploader.upload(file)
of the archive views.py
Follows code
models.py
from django.db import models
from django.utils import timezone
from cloudinary.models import CloudinaryField
class Post(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
text = models.TextField()
create_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
image_url = CloudinaryField('imagem', blank=True, null=True)
def publish(sefl):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title
views.py
def post_new(request):
upload_result = None
thumbnail = None
if request.method == "POST":
form = PostForm(request.POST, request.FILES)
if form.is_valid():
file = request.FILES['image_url']
if file:
upload_result = cloudinary.uploader.upload(file)
thumbnail, option = cloudinary_url(upload_result['public_id'], format="jpg", crop="fill", width=100, height=100)
post = form.save(commit=False)
post.author = request.user
post.published_date = timezone.now()
post.save()
return redirect('post_detail', pk=post.pk)
else:
form = PostForm()
return render(request, 'blog/post_edit.html', {'form': form})
post_edit.html
{% extends 'blog/base.html' %}
{% load cloudinary %}
{% block content %}
<h1>Nova postagem</h1>
<form action="" method="post" class="post-form" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_table }}
<br />
<div class="col-md-3">
<input type="submit" value="Salvar" class="btn btn-success">
</div>
</form>
{% endblock %}
If anyone can, include the tag cloudinary.
Thank you so much! It worked!
– Leonardo Nascimento Cintra