在@login_required之后得到下一个 [英] get next after @login_required

查看:162
本文介绍了在@login_required之后得到下一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些不明原因,我目前无法从 @login_required 重定向访问下一个值。我附上了我的代码login.html页面和我的项目设置



输入 name = next value ='',而它应该包含从 {{next}} 中的值,它在调试转储中显示为: p>

  GET数据
变量值
u'next'[u'/ accounts / profile /']

login.html

  {%extendsbase.html%} 

{%block content%}

{%if form.errors%}
< p class =error>对不起,您输入的用户名或密码不正确< / p>
{%endif%}
< form action =/ accounts / auth /method =post> {%csrf_token%}
< label for =username> ;用户名:< / label>
< input type =textname =usernamevalue =id =username>

< label for =password>密码:< / label>
< input type =passwordname =passwordvalue =id =password>

< input type ='text'name =nextvalue ={{next}}>
< input type =submitvalue =login>
< / form>

{%endblock%}

/ code>:

 #在项目中构建路径,如下所示:os.path.join(BASE_DIR,.. 。)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__ file__))

#安全警告:不要运行调试打开生产!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


#应用程序定义

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 .crfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware'


ROOT_URLCONF ='django_yunite.urls'

WSGI_APPLICATION ='django_yunite.wsgi.a pplication'

#国际化
#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


#静态文件(CSS,JavaScript,图像)
#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'
$ b从django.conf导入global_settings
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS +(
django.core.context_processors.request,

views.py

  def login(request):
c = {}
c.update(csrf(request))
return render_to_response('login.html',c)


解决方案

尝试在您的上下文中添加 next 参数。




c = {'next':request.GET.get('next','/')}
return render(请求,'login.html',c)

这里有一些额外的注释:


  1. 您可能想使用 render 快捷方式或提供 RequestContext 作为 context_instance 您的 render_to_response 调用的参数。


  2. Django的内置 login 视图应该处理这个,除非你需要特殊的逻辑。编写自己的观点没有任何错误,但是Django提供了很多开箱即用的功能,可以减少编写自己需要的代码量。内置的视图也具有经过良好测试和通用的好处。



I am currently unable to gain access to the next value from the @login_required redirection for some untold reason. I have attached my code for the login.html page and my project settings

input name=next value='' whereas it should contain the value from {{ next }} which is shown in the debug dump as:

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

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="{{ next }}">
    <input type="submit" value="login">
  </form>

{% endblock %}

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'

from django.conf import global_settings
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
        "django.core.context_processors.request",
) 

views.py:

def login(request):
  c={}
  c.update(csrf(request))
  return render_to_response('login.html', c)

解决方案

Try adding the next parameter to your context.

def login(request):
    c = {'next' : request.GET.get('next', '/')}
    return render(request, 'login.html', c)

A couple of extra notes here:

  1. You probably want to use the render shortcut or provide a RequestContext as the context_instance argument to your render_to_response call.

  2. Django's builtin login view should handle this for you unless you need special logic. There's nothing wrong with writing your own views, but Django provides a lot of functionality out of the box to reduce the amount of code you need to write yourself. The builtin views also have the benefit of being well tested and generic.

这篇关于在@login_required之后得到下一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆