These are chat archives for django/django

23rd
Jul 2018
Curtis Maloney
@funkybob
Jul 23 2018 07:04
"apps" in Django are mostly just an organisational thing, @felyx
they don't control URL patterns, for instance
matfire
@matfire
Jul 23 2018 09:50

hi guys, I'm trying to grab a gravatar image for a user on my website, but out of nowhere I get the following error: str object has no attribute "email"
the code that causes problems is the following line:
email = str(user.email.strip().lower()).encode('utf-8')

Can you help me? I changed nothing on the function itself, but just added a new template

Curtis Maloney
@funkybob
Jul 23 2018 09:58
@matfire not without at least a traceback
and the code... what is 'user' at that point?
matfire
@matfire
Jul 23 2018 09:59

Sure @funkybob
Here's the traceback:

Internal Server Error: /user/1/modifica/
Traceback (most recent call last):
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, callback_args, **callback_kwargs)
File "/home/matteo/CMS/amministrazione/views.py", line 59, in modifica
return render(request, "utenti_modifica.html", context)
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/template/base.py", line 175, in render
return self._render(context)
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/template/base.py", line 167, in _render
return self.nodelist.render(context)
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/template/base.py", line 943, in render
bit = node.render_annotated(context)
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
return self.render(context)
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/template/loader_tags.py", line 155, in render
return compiled_parent._render(context)
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/template/base.py", line 167, in _render
return self.nodelist.render(context)
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/template/base.py", line 943, in render
bit = node.render_annotated(context)
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
return self.render(context)
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/template/loader_tags.py", line 67, in render
result = block.nodelist.render(context)
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/template/base.py", line 943, in render
bit = node.render_annotated(context)
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
return self.render(context)
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/template/base.py", line 993, in render
output = self.filter_expression.resolve(context)
File "/home/matteo/virtualenvs/StageTek2/lib/python3.6/site-packages/django/template/base.py", line 703, in resolve
new_obj = func(obj,
arg_vals)
File "/home/matteo/CMS/main/templatetags/main_tags.py", line 9, in gravatar
email = str(user.email.strip().lower()).encode('utf-8')
AttributeError: 'str' object has no attribute 'email'

and the user at that point is either the one included in the request variable, or a selected one (profile page). the function is this one
def gravatar(user, size=35): email = str(user.email.strip().lower()).encode('utf-8') email_hash = md5(email).hexdigest() url = "//www.gravatar.com/avatar/{0}?s={1}&d=identicon&r=PG" return url.format(email_hash, size)

and I use it like this in a template(it is registered as a filter)
{{profile|gravatar:50}} where profile is a user object

Curtis Maloney
@funkybob
Jul 23 2018 10:01
perhaps also learn to use ``` for quoting code blocks :)
so... in your template context somehow 'profile' (which you, for some reason, passed to a filter that expects user) is a str
matfire
@matfire
Jul 23 2018 10:03
yes, but it not supposed to be an str, since profile is a User object
Curtis Maloney
@funkybob
Jul 23 2018 10:06
so, what is your view? what's is passing in as 'profile'?
matfire
@matfire
Jul 23 2018 10:08
profile is a User, which is selected using the get_object_or_404 function
Curtis Maloney
@funkybob
Jul 23 2018 10:11
show
the
code
I don't trust you
since you can't debug this yourself, you don't know where the problem is [by definition]
so I don't want to know your interpretation of what things are
I want to see the code
matfire
@matfire
Jul 23 2018 10:13
def detail(request, id=None):
    if not request.user.is_authenticated:
        return redirect("index")
    instance = get_object_or_404(User, pk=id)
    form = UserChangeForm(request.POST or None, instance=instance)
    context = {}
    if form.is_valid():
        instance = form.save(commit=False)
        if form.cleaned_data.get("password1") and form.cleaned_data.get("password2"):
            if form.cleaned_data.get("password1") == form.cleaned_data.get("password2"):
                instance.set_password(form.cleaned_data.get("password1"))
                if request.user.id == instance.id:
                    update_session_auth_hash(request, instance)
        instance.save()
        form.save_m2m()
        action.send(request.user, verb="ha modificato l'utilizzatore", target=instance, ip=get_ip(request))
        return redirect("/")
    context["title"] = instance.first_name
    context["form"] = form
    context["profile"] = instance
    context["parent"] = "Amministrazione"
    return render(request, "profile.html", context)
this is the responsible view
Curtis Maloney
@funkybob
Jul 23 2018 10:16
so an authenticated use may change any other user? how odd...
matfire
@matfire
Jul 23 2018 10:16
yes, for the time being there is no control whether or not the viewed profile is the current user profile
it is however something that will be implented
Curtis Maloney
@funkybob
Jul 23 2018 10:19
def detail(request, id=None):
    if not request.user.is_authenticated:
        return redirect("index")
    user = get_object_or_404(User, pk=id)
    form = UserChangeForm(request.POST or None, instance=user)
    if request.method == 'POST':
        if form.is_valid():
            user = form.save(commit=False)
            user.set_password(form.cleaned_data.get("password1"))
            if request.user.id == instance.id:
                update_session_auth_hash(request, instance)
            user.save()
            form.save_m2m()
            action.send(request.user, verb="ha modificato l'utilizzatore", target=user, ip=get_ip(request))
            return redirect("/")

    return render(request, "profile.html", {
        "title": instance.first_name,
        "form": form,
        "profile": user,
        "parent": "Amministrazione",
    })
you should move the password equality test into the form validation
now... does nothing in your template redeclare "profile", by chance/
also, what's the exact declaration of your filter?
matfire
@matfire
Jul 23 2018 10:20
no, of that I am sure
however I just emptied my browser's cache and now it works
Curtis Maloney
@funkybob
Jul 23 2018 10:22
ah
matfire
@matfire
Jul 23 2018 10:22
could it have been as stupid as that?
Curtis Maloney
@funkybob
Jul 23 2018 10:22
possibly...
matfire
@matfire
Jul 23 2018 10:22
ok...
thanks, Curtis