django下一个URL参数

发布于 2025-01-21 06:46:58 字数 2409 浏览 0 评论 0原文

我正在使用自定义登录视图和自定义登录页面。我正在努力使用的是我要使用的下一个参数,因此,如果URL中有下一个参数,则用户将被重定向到上一页。

如下所示,我正在使用django-crispy-Formsdjango-axes

问题在于,我能够成功地将用户重定向到主页,但是我无法访问任何可见的数据(所有视图均限制了登录用户),例如用户电子邮件和内容。此外,当我尝试单击任何超链接Django时,将自动重定向到登录页面。

当我尝试手动访问页面时,例如它将我重定向到http://127.0.0.1:8000/accounts/login/?next=/articles/它给我

找不到页面(404)请求方法:获取请求URL :http://127.0.0.0.1:8000/accounts/login/?next=/articles/

views.py

class UpdatedLoginView(LoginView):
    form_class = LoginForm
    template_name = 'user/login.html'
    redirect_field_name='main/homepage.html'
    
    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():
            if 'next' in request.POST:
                return redirect(request.POST.get('next'))
            else:
                return(redirect('homepage'))
        else:
            return render(request, self.redirect_field_name, {'form': form})

class ArticleListView(LoginRequiredMixin, ListView):
    template_name = 'articles/ArticleListView.html'
    model = Article
    paginate_by = 6
    queryset = Article.objects.filter(status=1)
    
    def get_context_data(self, **kwargs):
        context = super(ArticleListView, self).get_context_data(**kwargs)
        return context

login.html

<form method="post" class="login-form background" novalidate>
    {% csrf_token %}
    {{ form|crispy }}
    {% if request.GET.next %}
        <input type="hidden" name="next" value="{{ request.GET.next }}"/>
    {% endif %}
    <button type="submit" class="text-center mx-auto login-btn">Login</button>
</form>

urls.py

urlpatterns = [
    path('', views.UpdatedLoginView.as_view(), name='login'),
    path('logout/', views.logout_view, name='logout'),
    path('home/', views.HomePageView.as_view(), name='homepage'),
    path('articles/', views.ArticleListView.as_view(), name='article_list'),
]

setters.py.py

#AUTHORIZATION
AXES_FAILURE_LIMIT = 4
AXES_ONLY_USER_FAILURES = True
AXES_ENABLE_ADMIN = True
LOGIN_REDIRECT_URL = 'homepage'
LOGOUT_REDIRECT_URL = 'login'

您可以检查代码,让我知道我是什么做错了吗?也许它与设置有关,但我不确定这是什么问题。我需要将下一个参数传递给所有视图吗?如果是,最好的方法是什么?

I am using a custom Login View and a custom login page. What I am struggling with is the next parameter that I want to use so if there is the next parameter in the url then the user is being redirected to the previous page.

As you can see in the code below I am using django-crispy-forms and django-axes.

The problem is that I am able to successfully redirect users to the homepage but I cannot access any data that is visible for logged-in users (all views are restricted for logged-in users) such as user email, and content. Moreover, when I try to click on any hyperlink Django is automatically redirecting to the login page.

When I try to access a page manually, for instance, http://127.0.0.1:8000/articles it redirects me to http://127.0.0.1:8000/accounts/login/?next=/articles/ which gives me

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/accounts/login/?next=/articles/

views.py

class UpdatedLoginView(LoginView):
    form_class = LoginForm
    template_name = 'user/login.html'
    redirect_field_name='main/homepage.html'
    
    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():
            if 'next' in request.POST:
                return redirect(request.POST.get('next'))
            else:
                return(redirect('homepage'))
        else:
            return render(request, self.redirect_field_name, {'form': form})

class ArticleListView(LoginRequiredMixin, ListView):
    template_name = 'articles/ArticleListView.html'
    model = Article
    paginate_by = 6
    queryset = Article.objects.filter(status=1)
    
    def get_context_data(self, **kwargs):
        context = super(ArticleListView, self).get_context_data(**kwargs)
        return context

login.html

<form method="post" class="login-form background" novalidate>
    {% csrf_token %}
    {{ form|crispy }}
    {% if request.GET.next %}
        <input type="hidden" name="next" value="{{ request.GET.next }}"/>
    {% endif %}
    <button type="submit" class="text-center mx-auto login-btn">Login</button>
</form>

urls.py

urlpatterns = [
    path('', views.UpdatedLoginView.as_view(), name='login'),
    path('logout/', views.logout_view, name='logout'),
    path('home/', views.HomePageView.as_view(), name='homepage'),
    path('articles/', views.ArticleListView.as_view(), name='article_list'),
]

settings.py

#AUTHORIZATION
AXES_FAILURE_LIMIT = 4
AXES_ONLY_USER_FAILURES = True
AXES_ENABLE_ADMIN = True
LOGIN_REDIRECT_URL = 'homepage'
LOGOUT_REDIRECT_URL = 'login'

Can you please check the code and let me know what am I doing wrong? Maybe it is related to settings but I a not sure what is the issue here. Do I need to pass the next parameter to all views? If yes what is the best way to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水微香 2025-01-28 06:46:58

在您的应用程序中,login_url设置为默认值,即“/accounts/login/”

但是“/encace/login/”与视图无关,因此Web服务器返回404(找不到)。

解决该问题的一种方法是通过将以下行添加到urlpatterns:

path(“ account/”,inclage(“ django.contrib.auth.urls”)),>

in your app, LOGIN_URL is set to the default value i.e. "/accounts/login/"

but "/accounts/login/" is not associated to a view, thus the web server returns 404 (not found).

one way to solve the issue is to include the standard login view by adding the following line to urlpatterns:

path("accounts/", include("django.contrib.auth.urls")),

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文