Django nach @login_required Umleitung zur nächsten

Ich glaube, das ist eine einfache Frage, und ich bin fehlt nur noch 1 kleinen Schritt.

Ich wollen beliebige Anzahl der folgenden (wie der Begriff in dem nächsten parameter):

[not signed in] -> profile -> login?next=/accounts/profile/ -> auth -> profile.
[not signed in] -> newsfeed -> login?next=/newsfeed/` -> auth -> newsfeed.

Wogegen ich mich derzeit:

[not signed in] -> profile -> login?next=/accounts/profile/ -> auth -> loggedin
[not signed in] -> newsfeed -> login?next=/newsfeed/ -> auth -> loggedin

Ich bin auf der Suche irgendwie vorbei die next parameter aus einem Formular auf login zu auth und haben auth Weiterleitung dieser parameter

Zur Zeit versuche ich in meinem login.html:

<input type='text' name="next" value="{{ next }}">

dies ist jedoch nicht immer der nächste Wert. Ich kann sehen, aus dem debug tool bar:

GET data
Variable    Value
u'next'     [u'/accounts/profile/']

views:

def auth_view(request):
  username = request.POST.get('username', '')
  password = request.POST.get('password', '')
  user = auth.authenticate(username=username, password=password)

  if user is not None:
    auth.login(request, user)
    print request.POST
    return HttpResponseRedirect(request.POST.get('next'),'/accounts/loggedin')
  else:
    return HttpResponseRedirect('/accounts/invalid')

login.html:

{% extends "base.html" %}

{% block content %}

  {% if form.errors %}
  <p class="error"> Sorry, you have entered an incorrect username or password</p>
  {% endif %}
  <form action="/accounts/auth/" method="post">{% csrf_token %}
    <label for="username">User name:</label>
    <input type="text" name="username" value="" id="username">

    <label for="password">Password:</label>
    <input type="password" name="password" value="" id="password">

    <input type='text' name="next" value="{{ request.GET.next }}">
    <input type="submit" value="login">
  </form>

{% endblock %}

settings:

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:

    url(r'^admin/', include(admin.site.urls)),
    ('^accounts/', include('userprofile.urls')),

    url(r'^accounts/login/$', 'django_yunite.views.login'),
    url(r'^accounts/auth/$', 'django_yunite.views.auth_view'),
    url(r'^accounts/logout/$', 'django_yunite.views.logout'),
    url(r'^accounts/loggedin/$', 'django_yunite.views.loggedin'),
    url(r'^accounts/invalid/$', 'django_yunite.views.invalid_login'),

)

settings:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'debug_toolbar',
    'userprofile',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'django_yunite.urls'

WSGI_APPLICATION = 'django_yunite.wsgi.application'

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-ca'

TIME_ZONE = 'EST'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    ('assets', '/home/user/GitHub/venv_yunite/django_yunite/static/'),
    )

TEMPLATE_DIRS = (
    './templates',
    '/article/templates',
)

STATIC_ROOT = "/home/user/Documents/static/"

AUTH_PROFILE_MODULE = 'userprofile.UserProfile'

die print-Anweisung zeigt eine leere u'next'

  • Kümmern Sie sich um Ihre ), return HttpResponseRedirect(request.POST.get('next'),'/accounts/loggedin') sollte return HttpResponseRedirect(request.POST.get('next','/accounts/loggedin'))
  • was Nein? warum? auch das löst nicht das Problem. Das Problem befindet, aus welchem Grund auch immer den 'weiter' - parameter ist leer. die {{ request.BEKOMMEN.nächste }} ist nicht wieder alles
  • Aktualisieren Sie die Frage mit Ihrem settings.py.
  • Hinzugefügt settings.py
  • Der Grund dafür, dass es nicht funktioniert, wie hervorgehoben, in meiner Antwort ist, dass Sie fehlen die django.core.context_processors.request in Ihrem TEMPLATE_CONTEXT_PROCESSORS Einstellung. Ich update meine Antwort um zu zeigen, wie Sie diese.
InformationsquelleAutor Matt Stokes | 2014-02-11
Schreibe einen Kommentar