我试图在用户登录django网站时向他们打招呼,但是使用django登录使发送消息到我的模板并重定向到幕后的新URL太困难了。如何在重定向到的网址模板上向用户(最好是该用户名)打招呼?
我尝试使用消息,但是重定向到新的URL总是会覆盖消息,并且永远不会出现。定期登录和注销都可以,我只是想弄清楚如何向用户显示欢迎文本,以使他们知道他们已经成功登录并正在使用自己的帐户。
views.py
def loginPage(request):
return render(request, "registration/login.html")
def login(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
#Success!
messages.success(request, "Welcome " + user) #NOT WORKING :(
login(request, user)
else:
return HttpReponseRedirect("/DataApp/accounts/login/")
settings.py
# Application definition
INSTALLED_APPS = [
'DataApp.apps.DataappConfig',
'session_security',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'session_security.middleware.SessionSecurityMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'WebscrapeApp.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '/DataApp/templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
"django.template.context_processors.debug",
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'WebscrapeApp.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/New_York'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
LOGIN_URL = "/DataApp/accounts/login/"
LOGIN_REDIRECT_URL = "/DataApp/search/"
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SESSION_SECURITY_WARN_AFTER = 900
SESSION_SECURITY_EXPIRE_AFTER = 1200
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
base.html
<!-- This is nested in a div tag inside of the html class body -->
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}" {% endif %}>
{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
我希望消息在传递后显示在base.html中,但是django的登录功能似乎会在后台隐藏消息的发送。
解决方案:
base.html
<p>
{% if not user.is_anonymous %}
<font color="#00ccff">
Welcome {{ user }}
</font>
{% endif %}
</p>
views.html
def login_view(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
#Success!
login(request, user)
context = {
"user": user
}
return render(request, "base.html", context)
else:
return HttpReponseRedirect("/DataApp/accounts/login/")
答案 0 :(得分:0)
首先重命名登录功能,例如login_view等。 如果那不能解决您的问题,也可以这样做
if user is not None:
login(request, user)
context = {
"user": user
}
return render(request, "base.html", context)
{% if user %}
Welcome {{ user }}
{% endif %}