开发者

Can't get django-registration to redirect to ?next after login

开发者 https://www.devze.com 2023-03-08 07:20 出处:网络
This seems like a really simple problem, but I can\'t get it to work. django-registration will pass in the correct ?next=..... if the @login_required decorator is present in the particular view functi

This seems like a really simple problem, but I can't get it to work. django-registration will pass in the correct ?next=..... if the @login_required decorator is present in the particular view function. Once the user logs in, the user will be redirected to the page he wanted to look at. This is good a开发者_如何学Pythonnd works properly.

My problem is I have this html page that doesn't require login, but if you want to log in, I have an <a href='{% url auth_login> present. If the user logs in from this html, the ?next= shows a blank and is redirected to the default location accounts/profile/. I haven't set up the user profile yet, so I get a TemplateDoesNotExist error.

I have something like this:

views.py

...
def display_all(request):
    response = list_detail.object_list(
               request,
               queryset = MyModel.objects.all(),
               template_name = 'show_all.html'
    )
    return response

-

show_all.html

<html>
<head>
          <title> Display Everything </title>
</head>
<body>
          <a href='{% url auth_login %}?next={{ request.path }}'>Login</a>
          <h1> Displaying all items! </h1>
          <ul>
                  {% for item in object_list %}
                       <li> {{ item.name }} </li>
                  {% endfor %}
          </ul>
</body>
</html>

Here, I display everything by listing it out and I have a login link at the top if you want to log in. {{ request.path }} returns an empty string, so the end result I get is http://localhost:8000/accounts/login/?next= which will redirect to the default accounts/profile/.


If you problem is that a request.path is empty, than you need to add a "django.core.context_processors.request" to TEMPLATE_CONTEXT_PROCESSORS in settings.py. Look at django docs


A tuple of callables that are used to populate the context in RequestContext. These callables take a request object as their argument and return a dictionary of items to be merged into the context.

TEMPLATE_CONTEXT_PROCESSORS Default: https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors

0

精彩评论

暂无评论...
验证码 换一张
取 消