'django next url parameter
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?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
