我正在为CS50 W(网络)工作4。我有一个奇怪的问题,我刚刚注意到:
网址:urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("login", views.login_view, name="login"),
path("logout", views.logout_view, name="logout"),
path("register", views.register, name="register"),
path("create_post", views.create_post, name="create_post"),
path("<str:username>", views.profile, name="profile")
]
字符串
视图给出问题:
def profile(request, username):
user = User.objects.get(username=username)
posts = Post.objects.filter(owner=user).order_by('-timestamp')
print(f'Current username: {user.username} ({user.email})')
return render(request, 'network/profile.html', {
'username': user,
'followed': user.followers.filter(username=request.user.username),
'posts': posts
})
型
终端日志:
**Current username: FernandoBorea (fernando@email.com)**
[26/Jul/2023 00:40:25] "GET /FernandoBorea HTTP/1.1" 200 2651
Internal Server Error: /favicon.ico
Traceback (most recent call last):
File "/home/fernandoborea/.local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/fernandoborea/.local/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/fernandoborea/studies/CS50W/TestingCICD/project4/cs50w-project4/network/views.py", line 104, in profile
user = User.objects.get(username=username)
File "/home/fernandoborea/.local/lib/python3.10/site-packages/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/fernandoborea/.local/lib/python3.10/site-packages/django/db/models/query.py", line 637, in get
raise self.model.DoesNotExist(
network.models.User.DoesNotExist: User matching query does not exist.
[26/Jul/2023 00:40:26] "GET /favicon.ico HTTP/1.1" 500 71927
型
正如你所看到的,user变量在打印到终端时确实获得了一个User示例,站点上的一切都很完美,但我不知道为什么我会在终端上报告错误。另一件奇怪的事情是,在加载默认路由(索引)时,它也显示错误,即使没有调用给出问题的视图。
任何帮助是高度赞赏!
更新更多日志
出于某种原因,即使访问索引路由('/'),它仍然显示来自未被请求的视图的错误。
附加日志,这次它显示加载索引页面,然后导航到用户的页面:
[26/Jul/2023 01:52:40] "GET / HTTP/1.1" 200 3667
Internal Server Error: /favicon.ico
Traceback (most recent call last):
File "/home/fernandoborea/.local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/fernandoborea/.local/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/fernandoborea/studies/CS50W/TestingCICD/project4/cs50w-project4/network/views.py", line 104, in profile
user = User.objects.get(username=username)
File "/home/fernandoborea/.local/lib/python3.10/site-packages/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/fernandoborea/.local/lib/python3.10/site-packages/django/db/models/query.py", line 637, in get
raise self.model.DoesNotExist(
network.models.User.DoesNotExist: User matching query does not exist.
[26/Jul/2023 01:52:42] "GET /favicon.ico HTTP/1.1" 500 71940
Current username: FernandoBorea (fernando@email.com) (True)
[26/Jul/2023 01:52:44] "GET /FernandoBorea HTTP/1.1" 200 2651
Internal Server Error: /favicon.ico
Traceback (most recent call last):
File "/home/fernandoborea/.local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/fernandoborea/.local/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/fernandoborea/studies/CS50W/TestingCICD/project4/cs50w-project4/network/views.py", line 104, in profile
user = User.objects.get(username=username)
File "/home/fernandoborea/.local/lib/python3.10/site-packages/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/fernandoborea/.local/lib/python3.10/site-packages/django/db/models/query.py", line 637, in get
raise self.model.DoesNotExist(
network.models.User.DoesNotExist: User matching query does not exist.
[26/Jul/2023 01:52:44] "GET /favicon.ico HTTP/1.1" 500 71953
型
更新2,型号
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
id = models.AutoField(primary_key=True)
following = models.ManyToManyField(to='User', related_name='followers_relname', symmetrical=False, blank=True)
followers = models.ManyToManyField(to='User', related_name='following_relname', symmetrical=False, blank=True)
class Post(models.Model):
id = models.AutoField(primary_key=True)
owner = models.ForeignKey(to=User, on_delete=models.CASCADE, related_name='owner')
post = models.TextField(max_length=1000)
timestamp = models.DateTimeField(auto_now_add=True)
likes = models.ManyToManyField(to=User, related_name='liked_posts', blank=True)
def __str__(self):
return f'{self.owner}: {self.post}'
型
1条答案
按热度按时间r7xajy2e1#
关于回溯的值得注意的是,错误实际上是在图像调用之后**出现的。
配置文件视图的路径
path("<str:username>", views.profile, name="profile")
基本上匹配ANY字符串。你知道什么也是绳子吗
/favicon.ico
。您的URL路径实际上与图像请求匹配!你需要让这条路更有区别。我认为错误应该会消失如果您用当前路径替换为
path("<str:username>/profile", views.profile, name="profile")
个并且改变了任何需要改变的链接。