realpython / realpython/materials

[Patch] Dtwitter crash if user not logged

Abierto
#443 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Lenguaje dominante
Jupyter Notebook
Estrellas
5.2k
Forks
5.3k
Merge medio
4 d 10 h
PR fusionados (30 d)
10

Descripción

ℹ️ Please note that the best way to get support for Real Python courses & articles is to join one of our weekly Office Hours calls or in the RP Community Slack.

You can report issues and problems here, but we typically won't be able to provide 1:1 support outside the channels listed above.

Describe the bug
If you get from part-4 the source final and load in the browser you get:

Traceback (most recent call last):
  File "/var/www/dtwitter/myvenv/lib/python3.11/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/dtwitter/myvenv/lib/python3.11/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/dtwitter/dwitter/views.py", line 17, in dashboard
    user__profile__in=request.user.profile.follows.all()
                      ^^^^^^^^^^^^^^^^^^^^
  File "/var/www/dtwitter/myvenv/lib/python3.11/site-packages/django/utils/functional.py", line 247, in inner
    return func(self._wrapped, *args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'AnonymousUser' object has no attribute 'profile'

Fix

The first lines of the file https://github.com/realpython/materials/blob/master/dwitter-part-4/source_code_final/dwitter/views.py#L7 should be:

from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required

from .forms import DweetForm
from .models import Dweet, Profile

@login_required(login_url='/admin/')
def dashboard(request):
    form = DweetForm(request.POST or None)

In this way automatically if the user is not logged get redirect to the admin for login, maybe there are better ways as it is just a prototype, but a prototype shouldn't crash.

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Abre dwitter-part-4/source_code_final/dwitter/views.py e inspecciona la vista del dashboard alrededor del acceso a request.user.profile. Reproduce la solicitud del navegador sin autenticación y verifica que redirige al inicio de sesión del administrador en lugar de generar la excepción AnonymousUser.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
django, python
Área
authentication, backend
Tipo de issue
Error
Dificultad
1/5
Tiempo estimado
Menos de una hora
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.