realpython / realpython/materials

[Patch] Dtwitter crash if user not logged

Aperta
#443 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Jupyter Notebook
Stelle
5.2k
Fork
5.3k
Merge medio
4g 10h
PR unite (30g)
10

Descrizione

ℹ️ 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.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Apri dwitter-part-4/source_code_final/dwitter/views.py e analizza la view del dashboard intorno all'accesso a request.user.profile. Riproduci la richiesta del browser non autenticata, quindi verifica che reindirizzi al login dell'amministratore invece di generare l'eccezione AnonymousUser.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
django, python
Ambito
authentication, backend
Tipo di issue
Bug
Difficoltà
1/5
Tempo stimato
Meno di un'ora
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.