kan
kan

Reputation: 45

Django I want to get avatar from Twitter but it fails

Django2.1 I have implemented twitter authentication in social-auth-app-django. I want to get an avatar for login account, but it doesn't work.

I added a pipeline referring to this Japanese page, but I get an error. What is the reason?It works fine before writing a pipeline.

ModuleNotFoundError at /complete/twitter/
No module named 'myapp.users'

my code

#settings.py
SOCIAL_AUTH_PIPELINE = (
    'social_core.pipeline.social_auth.social_details',
    'social_core.pipeline.social_auth.social_uid',
    'social_core.pipeline.social_auth.auth_allowed',
    'social_core.pipeline.social_auth.social_user',
    'social_core.pipeline.user.get_username',
    'social_core.pipeline.user.create_user',
    'social_core.pipeline.social_auth.associate_user',
    'social_core.pipeline.social_auth.load_extra_data',
    'social_core.pipeline.user.user_details',
    'myapp.users.pipeline.get_avatar',
)
#pipiline.py
def get_avatar(backend, strategy, details, response,
               user=None, *args, **kwargs):
    url = None

    if backend.name == 'twitter':
        url = response.get('profile_image_url', '').replace('_normal', '')

    if url:
        user.profile.avatar = url
        user.save()

#models.py
from django.contrib.auth import get_user_model
from django.db import models

def get_myapp_image_path(instance, filename):
    return 'image-{0}/{1}'.format(instance.id, filename)

class Social(models.Model):
    user = models.ForeignKey(get_user_model(),
                             related_name='socials',
                             on_delete=models.CASCADE)
    provider = models.CharField(max_length=32)
    uid = models.CharField(max_length=255)

    class Meta:
        unique_together = ('provider', 'uid')
        db_table = 'socials'

class Myapp(models.Model):
    user = models.OneToOneField(get_user_model(), on_delete=models.CASCADE)
    username = models.CharField(max_length=30, unique=True, primary_key=True)
    title = models.CharField(max_length=20)
    content = models.TextField(max_length=500)
    avatar = models.URLField(max_length=200, blank=True)
    posted_date = models.DateTimeField(auto_now=True)

    def save(self, *args, **kwargs):
        if not self.username:
            self.username = self.user.username

        super(Myapp, self).save(*args, **kwargs)

thank you.

---add----

#settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'bootstrap4',
    'myapp.apps.MyappConfig',
    'social_django',
]

My project directory structure.

Projects
-assets
-media
-projects
    —__init__.py
    —settings.py
    —urls.py
    —wigs.py
-myapp
    —migrations
    —__init__.py
    —admin.py
    —apps.py
    —models.py
    —pipeline.py
    —tests.py
    —views.py

Upvotes: 0

Views: 169

Answers (1)

Aman Garg
Aman Garg

Reputation: 2547

You should use myapp.pipeline.get_avatar, instead of myapp.users.pipeline.get_avatar, since there is no module under myapp named users.py.

Upvotes: 1

Related Questions