0

PythonAnywhere 提供NOREVERSEMATCH/blog但 locahost 运行良好。

我已经去重新保存了该项目并再次将其提交到 GitHub 以及将其拉到 PythonAnywhere。但仍然无法正常工作。我试图重新加载它,但仍然在做同样的事情。

NoReverseMatch at /blog/
Reverse for 'profile' with arguments '('',)' not found. 1 pattern(s) tried: ['user\\/(?P<username>[^/]+)\\/profile\\/$']
4

1 回答 1

1

看起来您正在尝试链接到登录用户的个人资料 - 即使没有登录用户。

通常,您可以这样解决问题:

{% if user.is_authenticated %}
    <div>Hi user {{user.username }}!</div>
{% else %}
    <div><a href="{% url 'account-login' %}">Login here</a></div>
{% endif %}

这假设您的上下文处理器settings.py包含RequestContext

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [os.path.join(BASE_DIR, "templates"),],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",  # <=========== that must be there
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

上面链接的文档页面有很多关于与用户合作的示例和背景信息。

于 2020-07-26T18:54:11.600 回答