realpython / realpython/materials

[Patch] Dtwitter crash if user not logged

Open
#443 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
5.2k
Forks
5.3k
Avg merge
4d 10h
Merged PRs (30d)
10

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open dwitter-part-4/source_code_final/dwitter/views.py and inspect the dashboard view around the request.user.profile access. Reproduce the unauthenticated browser request, then verify that it redirects to the admin login instead of raising the AnonymousUser exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
authentication, backend
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.