[Patch] Dtwitter crash if user not logged
还没有人认领这个 Issue。
评估
- 难度
- 1/5
- 预计耗时
- 1 小时以内
- 新手友好度
- 45/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
- 技术栈
- django, python
调研方向
打开 dwitter-part-4/source_code_final/dwitter/views.py,检查 dashboard view 中 request.user.profile 访问附近的代码。重现未经过身份验证的浏览器请求,然后验证它会重定向到管理员登录页面,而不是引发 AnonymousUser 异常。
由索引模型根据 Issue 内容生成。
描述
ℹ️ 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.
- 主要语言
- Jupyter Notebook
- 星标
- 5.2k
- 派生
- 5.3k
- 平均合并
- 4 天 10 小时
- 30 天内合并 PR
- 10
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
realpython/materials 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
realpython/materials#729 ·
-
п 未关闭
难度 5/5 一周以上 新手友好度 1/100
realpython/materials#763 ·
-
难度 1/5 1 小时以内 新手友好度 55/100
realpython/materials#738 ·
-
难度 1/5 1 小时以内 新手友好度 45/100
realpython/materials#737 ·
-
проект2 未关闭
难度 5/5 一周以上 新手友好度 15/100
realpython/materials#725 ·
查看 realpython/materials 的全部 Issue
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 92/100
-
难度 2/5 1-3 小时 新手友好度 84/100
corazawaf/coraza-nginx#140 ·
-
documentation help wanted
难度 2/5 1-3 小时 新手友好度 88/100
-
难度 2/5 1-3 小时 新手友好度 86/100
clerk/javascript#9852 ·
-
难度 2/5 1-3 小时 新手友好度 62/100
AiursoftWeb/AnduinOS-2#19 ·