当我登录并重定向到 'next' 参数时,尾部斜杠会自动添加到 url,使其成为双斜杠

When ever I log in and I am redirect to the 'next' parameter, a trailing slash is automatically added to the url, making it double slashes

我在 Django 中创建了使用 LoginRequiredMixin 的视图。但是,每当我登录并被重定向到另一个 url 时,url 我被重定向到以多个斜杠结尾,而不是 django url 末尾通常的 1 个斜杠. 我的观点之一:

class BookListView(LoginRequiredMixin, ListView):

    model = Book
    # paginate enables the list view to fetch a certain number of records per page. This is
    # useful when the records are plenty and it is not possible to display all in one page.
    paginate_by = 3 

我的 login.html 模板:

{%extends 'catalog/base_generic.html'%}

{%block content%}

{%if form.errors %}
    <p>Your username and password didn't match. Please try again.</p>
{% endif %}

{%if next %}
    {% if user.is_authenticated %}
        <p>Your account doesn't have access to this page. To proceed,
            please login with an account that has access.
        </p>
    {% else %}
        <p>Please login to view this page.</p>
    {% endif %}
{% endif %}

<form method="POST", action="{% url 'login' %}">
    {% csrf_token %}
    <table>
        <tr>
            <td>{{form.username.label_tag}}</td>
            <td>{{form.username}}</td>
        </tr>
        <tr>
            <td>{{form.password.label_tag}}</td>
            <td>{{form.password}}</td>
        </tr>
    </table>
    <input type="submit" value="Login"/>
    <input type="hidden" name="next" value={{next}}/>
</form>

<P>
    <a href="{% url 'password_reset' %}">Forgot Password?</a>
</P>

{% endblock %}

当我刚刚登录时,一切似乎都很好: Login Page

但是,登录后,出现了这样的情况: Error Message with multiple slashes at the end

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/catalog/books///
Using the URLconf defined in locallibrary.urls, Django tried these URL patterns, in this order:

admin/
catalog/ [name='index']
catalog/ books/ [name='books']
catalog/ book/<int:pk>/ [name='book-detail']
catalog/ authors/ [name='authors']
catalog/ author/<int:pk>/ [name='author-detail']
accounts/
^static/(?P<path>.*)$
The current path, catalog/books///, didn’t match any of these.

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

我不知道我做错了什么。请帮忙!

你忘记了一些关于 {{ next }}

的引号

改为

<input type="hidden" name="next" value="{{next}}"/>
#settings.py

APPEND_SLASH: bool = True # by default

将其设置为 False 将解决您的额外斜线问题。检查文档;如果在生产中设置为 False 会有一些缺点。

利用 Doctests Module 调试导致问题的原因。

>>> from django.test import SimpleTestCase
>>> from django.test import Client
>>>
>>>
>>> class TestRedirect(SimpleTestCase):
...    """There is a good chance it will return redirect and 
...    and return 301 response; if APPEND_SLASH is causing the issue."""
>>>
>>>
>>>    def test_catalog_book(self):
...        client = Client()
...        response = client.get("/catalog/book/")
...        self.assertEqual(response.status_code, 301)

Read the documentation of advanced testings

>>> response = c.get('/redirect_me/', follow=True)
>>> response.redirect_chain
[('http://testserver/next/', 302), ('http://testserver/final/', 302)]