我正在尝试编写一个用于在 Django 中注销用户的测试。这是代码:
urls.py
from django.conf.urls import url
from django.contrib import admin
from accounts.views import LoginView, LogoutView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^login/', LoginView.as_view(), name='login'),
url(r'^logout/', LogoutView.as_view(), name='logout'),
]
Run Code Online (Sandbox Code Playgroud)
views.py
from django.http import HttpResponseRedirect
from django.contrib.auth import login, logout
from django.views.generic import View
class LogoutView(View):
def get(self, request):
logout(request)
return HttpResponseRedirect('/')
Run Code Online (Sandbox Code Playgroud)
tests.py
from django.test import TestCase, Client
from django.contrib.auth.models import User
class LogoutTest(TestCase):
def setUp(self):
self.client = Client()
self.user = User.objects.create_user(
username='user1',
email='user1_123@gmail.com',
password='top_secret123'
)
def test_user_logs_out(self):
self.client.login(email=self.user.email, password=self.user.password)
self.assertTrue(self.user.is_authenticated) …Run Code Online (Sandbox Code Playgroud) Method Not Allowed (GET): /users/logout/
Method Not Allowed: /users/logout/
[10/Dec/2023 12:46:21] "GET /users/logout/ HTTP/1.1" 405 0
Run Code Online (Sandbox Code Playgroud)
当我访问 url http://127.0.0.1:8000/users/logout/时发生这种情况
urls.py:
from django.contrib.auth import views as auth_views
urlpatterns = [
...other urls...
path('users/logout/', auth_views.LogoutView.as_view(), name='logout'),
]
Run Code Online (Sandbox Code Playgroud)
我期待用户注销
当我运行我的django测试时,我得到以下错误,这些错误在我的测试套件之外:
======================================================================
ERROR: test_known_user (django.contrib.auth.tests.remote_user.RemoteUserCustomTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.6/django/contrib/auth/tests/remote_user.py", line 160, in test_known_user
super(RemoteUserCustomTest, self).test_known_user()
File "/usr/lib/pymodules/python2.6/django/contrib/auth/tests/remote_user.py", line 67, in test_known_user
self.assertEqual(response.context['user'].username, 'knownuser')
TypeError: 'NoneType' object is unsubscriptable
======================================================================
ERROR: test_last_login (django.contrib.auth.tests.remote_user.RemoteUserCustomTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.6/django/contrib/auth/tests/remote_user.py", line 87, in test_last_login
self.assertNotEqual(default_login, response.context['user'].last_login)
TypeError: 'NoneType' object is unsubscriptable
======================================================================
ERROR: test_no_remote_user (django.contrib.auth.tests.remote_user.RemoteUserCustomTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.6/django/contrib/auth/tests/remote_user.py", line 33, in test_no_remote_user
self.assert_(isinstance(response.context['user'], AnonymousUser))
TypeError: 'NoneType' object is unsubscriptable
======================================================================
ERROR: …Run Code Online (Sandbox Code Playgroud) 我是django和python的完整n00b.我来自PHP背景,所以你必须接受我的道歉:p.
我正在尝试使用django中的管理面板功能向不同的人显示不同的选项.
系统应允许管理员将"项目"添加到列表中.然后,"开发人员"应该只能查看分配给它们的项目,并且只能更改某些字段.
所以我想这个问题有两个方面:
1)允许"开发者"登录管理系统的最佳方法吗?
1.a)如果是这样,我如何在管理员的用户表单上显示一个布尔字段?我只想标记is_developer.我已将其添加为userProfile但不了解如何在表单上显示它
2)我应该禁止他们登录(到管理面板)并制作"前端",他们只能看到他们被允许的内容吗?
我希望这是有道理的.我现在有点到处都是因为它完全偏离了我以前的习惯!
在此先感谢您提供的任何帮助:)
提前致谢.
我在我的一个Django网站上遇到了一个问题.经过身份验证的用户可以访问注册页面.但客户提出这是一个问题.所以我试图纠正这个问题并最终得到以下解决方案.
这是一个好的解决方案吗?或者我怎样才能做到好?
进程应该是这样的,当登录用户尝试访问注册页面时,他应该自动从站点注销,然后重定向到注册页面.
我的代码是
def user_signup(request, template_name='profiles/profile_register_form.html'):
if request.user.is_authenticated():
return custom_logout(request, next_page = "/accounts/register/")
def custom_logout(request, next_page='/'):
try:
language = request.session['django_language']
except:
language = False
response = logout(request, next_page=next_page)
if language:
request.session['django_language'] = language
return response
Run Code Online (Sandbox Code Playgroud) django django-urls django-authentication django-registration django-login
我有一个用Django构建的webapp.它适用于网络,但现在我正在构建一个Android应用程序.我不确定如何安全地验证Django后端的Android应用程序.
此webapp具有用户配置文件.用户可以使用Web界面注册/登录/注销.相关部分urls.py看起来像这样:
urlpatterns += patterns('',
url(r'^accounts/login/$', 'django.contrib.auth.views.login', name='login'),
url(r'^accounts/logout/$', 'django.contrib.auth.views.logout', name="logout"),
)
Run Code Online (Sandbox Code Playgroud)
我的理解是,在用户成功完成后accounts/login,在浏览器上存放了一些用于其余连接的cookie.它是否正确?
在Android设备上,给定用户名和密码,用户向Django后端验证用户的正确或最佳方式是什么?我是否需要像在浏览器中那样获取cookie或者有更好的方法吗?
我为我的django应用程序创建了一个User模型
class User(Model):
"""
The Authentication model. This contains the user type. Both Customer and
Business models refer back to this model.
"""
email = EmailField(unique=True)
name = CharField(max_length=50)
passwd = CharField(max_length=76)
user_type = CharField(max_length=10, choices=USER_TYPES)
created_on = DateTimeField(auto_now_add=True)
last_login = DateTimeField(auto_now=True)
def __unicode__(self):
return self.email
def save(self, *args, **kw):
# If this is a new account then encrypt the password.
# Lets not re-encrypt it everytime we save.
if not self.created_on:
self.passwd = sha256_crypt.encrypt(self.passwd)
super(User, self).save(*args, **kw)
Run Code Online (Sandbox Code Playgroud)
我还创建了一个身份验证中间件来使用这个模型.
from …Run Code Online (Sandbox Code Playgroud) 在我的Django项目中,我为每个django用户提供了一个配置文件,而配置文件与一个Info模型相关.两种关系都是OneToOne.由于大部分时间我都在为用户使用Profile和Info模型,因此我希望默认选择这些模型,这样我就不会再次访问数据库了.有没有办法使用Django身份验证?
在我的应用程序中,我需要通过我的REST API对用户进行身份验证.所以我有一个带有用户/通过字段的表单,在提交之后,我想直接进入"下一页".显然我需要通过AJAX提交表单,因为我不想被重定向到API页面.但是RemoteUserMiddleware,如果请求将由javascript处理,那么如何知道我的用户应该进行身份验证?
我正在使用django身份验证登录用户:
def signin(request):
if request.method == "POST":
username = request.POST.get("username").lower()
password = request.POST.get("password").lower()
user = authenticate(username = username, password=password)
Run Code Online (Sandbox Code Playgroud)
但是,我似乎无法在任何其他视图中访问当前用户.在每个模板中,我都可以访问用户,但我似乎无法访问视图本身.例如,在另一条路线中,我希望能够执行以下操作:
def profile(request):
skills = hasSkill.objects.filter(user__username=user.username)
return render(request, "/profile.html", {"skills" : skills})
Run Code Online (Sandbox Code Playgroud)
但我继续收到用户是Nonetype对象的错误.有任何想法吗?
django ×9
python ×2
ajax ×1
android ×1
django-admin ×1
django-login ×1
django-urls ×1
django-views ×1
ios ×1
iphone ×1
middleware ×1
prefetch ×1
python-3.x ×1
rest ×1
testing ×1