此处将为大家介绍关于我如何使用Deliciouspie登录django的详细内容,并且为您解答有关deliciousworld不能登录的相关问题,此外,我们还将为您介绍关于Django3.0:djan
此处将为大家介绍关于我如何使用Deliciouspie登录django的详细内容,并且为您解答有关delicious world不能登录的相关问题,此外,我们还将为您介绍关于Django 3.0:django-notifications给出错误:“ Notification.recipient”必须是“ User”实例、Django Deliciouspie不使用ManyToManyField更新资源、Django Deliciousp高级过滤:如何使用Q对象进行复杂的查找、Django 与 Apache 上传文件到项目得到 SuspiciousFileOperation 异常的有用信息。
本文目录一览:- 我如何使用Deliciouspie登录django(delicious world不能登录)
- Django 3.0:django-notifications给出错误:“ Notification.recipient”必须是“ User”实例
- Django Deliciouspie不使用ManyToManyField更新资源
- Django Deliciousp高级过滤:如何使用Q对象进行复杂的查找
- Django 与 Apache 上传文件到项目得到 SuspiciousFileOperation 异常
我如何使用Deliciouspie登录django(delicious world不能登录)
我正在尝试在自定义身份验证中覆盖is_authenticated。我有一些简单的东西(开始),像这样:
class MyAuthentication(BasicAuthentication): def __init__(self, *args, **kwargs): super(MyAuthentication, self).__init__(*args, **kwargs) def is_authenticated(self, request, **kwargs): return True
然后在我的ModelResource中
class LoginUserResource(ModelResource): class Meta: resource_name = ''login'' queryset = User.objects.all() excludes = [''id'', ''email'', ''password'', ''is_staff'', ''is_superuser''] list_allowed_methods = [''post''] authentication = MyAuthentication() authorization = DjangoAuthorization()
我不断收到500错误"error_message": "column username is not unique"
。我在数据库中只有一个用户名,这是我要验证的用户。
关于它为什么返回此错误的任何想法?我将如何允许api客户端登录?
答案1
小编典典你的方法将尝试使用你要进行身份验证的用户名创建一个新用户。正如你所注意到的,这将在数据库层冒泡,该用户已经存在。
你想要创建一个UserResource,在其上添加一个方法,用户可以将其发布到并使用通过用户名/密码传递的数据登录。
from django.contrib.auth.models import Userfrom django.contrib.auth import authenticate, login, logoutfrom tastypie.http import HttpUnauthorized, HttpForbiddenfrom django.conf.urls import urlfrom tastypie.utils import trailing_slashclass UserResource(ModelResource): class Meta: queryset = User.objects.all() fields = [''first_name'', ''last_name'', ''email''] allowed_methods = [''get'', ''post''] resource_name = ''user'' def override_urls(self): return [ url(r"^(?P<resource_name>%s)/login%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view(''login''), name="api_login"), url(r''^(?P<resource_name>%s)/logout%s$'' % (self._meta.resource_name, trailing_slash()), self.wrap_view(''logout''), name=''api_logout''), ] def login(self, request, **kwargs): self.method_check(request, allowed=[''post'']) data = self.deserialize(request, request.raw_post_data, format=request.META.get(''CONTENT_TYPE'', ''application/json'')) username = data.get(''username'', '''') password = data.get(''password'', '''') user = authenticate(username=username, password=password) if user: if user.is_active: login(request, user) return self.create_response(request, { ''success'': True }) else: return self.create_response(request, { ''success'': False, ''reason'': ''disabled'', }, HttpForbidden ) else: return self.create_response(request, { ''success'': False, ''reason'': ''incorrect'', }, HttpUnauthorized ) def logout(self, request, **kwargs): self.method_check(request, allowed=[''get'']) if request.user and request.user.is_authenticated(): logout(request) return self.create_response(request, { ''success'': True }) else: return self.create_response(request, { ''success'': False }, HttpUnauthorized)
现在你可以将POST发送到http://hostname/api/user/logindata
了 { ''username'' : ''me'', ''password'' : ''l33t'' }
。
Django 3.0:django-notifications给出错误:“ Notification.recipient”必须是“ User”实例
这是一个超级简单的解决方案。您已经99%到达那里!
代替此:
for book in books:
notify.send(Seller,recipient=book.seller,verb="User has just bought book named {} priced
as {}".format(book.title,book.price))
尝试一下:
for book in books:
notify.send(book.seller.user,recipient=book.seller.user,book.price))
Seller
具有一个名为user
的属性,但它本身不是user
。
Django Deliciouspie不使用ManyToManyField更新资源
为什么我的具有ManyToManyField的资源没有通过此PUT请求更新?
curl --dump-header - -H "Content-Type: application/json" -X PUT --data ''{"uuid":"blah","pass_token":"blah","favorites": ["/api/v1/organizations/1/"]}'' http://localhost:8000/api/v1/devices/2/
我得到这个回应:
HTTP/1.0 400 BAD REQUESTDate: Wed, 11 Jul 2012 22:21:15 GMTServer: WSGIServer/0.1 Python/2.7.2Content-Type: application/json; charset=utf-8{"favorites": ["\"/api/v1/organizations/1/\" is not a valid value for a primary key."]}
这是我的资源:
class OrganizationResource(ModelResource): parent_org = fields.ForeignKey(''self'',''parent_org'',null=True, full=True,blank=True) class Meta: allowed_methods = [''get'',] authentication = APIAuthentication() fields = [''name'',''org_type'',''parent_org''] filtering = { ''name'': ALL, ''org_type'': ALL, ''parent_org'': ALL_WITH_RELATIONS, } ordering = [''name'',] queryset = Organization.objects.all() resource_name = ''organizations''class DeviceResource(ModelResource): favorites = fields.ManyToManyField(OrganizationResource,''favorites'',null=True,full=True) class Meta: allowed_methods = [''get'',''patch'',''post'',''put'',] authentication = APIAuthentication() authorization = APIAuthorization() fields = [''uuid'',] filtering = { ''uuid'': ALL, } queryset = Device.objects.all() resource_name = ''devices'' validation = FormValidation(form_class=DeviceRegistrationForm)
获得OrganizationResource可以进行以下交换:
curl --dump-header - -H "Content-Type: application/json" -X GET http://localhost:8000/api/v1/organizations/1/HTTP/1.0 200 OKDate: Wed, 11 Jul 2012 22:38:30 GMTServer: WSGIServer/0.1 Python/2.7.2Content-Type: application/json; charset=utf-8{"name": "name", "org_type": "org_type", "parent_org": null, "resource_uri": "/api/v1/organizations/1/"}
这与django tastypie manytomany字段POSTjson错误非常相似,但是我在ManyToMany关系中未使用Through属性。
答案1
小编典典问题原来是验证方法。使用FormValidation意味着像/ api / v1 / organizations / 1
/这样的uri不会被验证为Django ORM的ForeignKey。使用自定义验证可以解决此问题。
许多博恩斯死了,以带给我们这些信息。
Django Deliciousp高级过滤:如何使用Q对象进行复杂的查找
我有一个基本的Django模型,例如:
class Business(models.Model):
name = models.CharField(max_length=200,unique=True)
email = models.EmailField()
phone = models.CharField(max_length=40,blank=True,null=True)
description = models.TextField(max_length=500)
我需要对上述模型执行复杂的查询,例如:
qset = (
Q(name__icontains=query) |
Q(description__icontains=query) |
Q(email__icontains=query)
)
results = Business.objects.filter(qset).distinct()
我已经尝试过使用 法式饼 ,但没有运气:
def build_filters(self,filters=None):
if filters is None:
filters = {}
orm_filters = super(BusinessResource,self).build_filters(filters)
if('query' in filters):
query = filters['query']
print query
qset = (
Q(name__icontains=query) |
Q(description__icontains=query) |
Q(email__icontains=query)
)
results = Business.objects.filter(qset).distinct()
orm_filters = {'query__icontains': results}
return orm_filters
在梅花类的Meta类中,我将过滤设置为:
filtering = {
'name: ALL,'description': ALL,'email': ALL,'query': ['icontains',],}
关于如何解决这个问题有什么想法吗?
谢谢-牛顿
Django 与 Apache 上传文件到项目得到 SuspiciousFileOperation 异常
如何解决Django 与 Apache 上传文件到项目得到 SuspiciousFileOperation 异常
我想将文件上传到 project/static/uploads
时出现错误。
SuspicIoUsFileOperation at /admin/myvideo/videomodel/add/
The joined path (D:\\Web\\pyproject\\xxm_hrproject_video\\src\\static\\uploads\\2499be8ca837405aa440a4a56065bbb0.mp4) is located outside of the base path component (D:\\Web\\Apache24)
如果上传到apache路径不会报错。
Django 项目路径为 D:\\Web\\pyproject\\xxm_hrproject_video\\src
Apache 路径为 D:\\Web\\Apache24
models.py
from uuid import uuid4
from django.conf import settings
class VideoModel(models.Model):
def wrapper(instance,filename):
# return r''D:\\Web\\Apache24\\static\\uploads\\123.mp4'' will not get any error
return r''D:\\Web\\pyproject\\xxm_hrproject_video\\src\\static\\uploads\\123.mp4''
file = models.FileField(upload_to=wrapper)
今天关于我如何使用Deliciouspie登录django和delicious world不能登录的分享就到这里,希望大家有所收获,若想了解更多关于Django 3.0:django-notifications给出错误:“ Notification.recipient”必须是“ User”实例、Django Deliciouspie不使用ManyToManyField更新资源、Django Deliciousp高级过滤:如何使用Q对象进行复杂的查找、Django 与 Apache 上传文件到项目得到 SuspiciousFileOperation 异常等相关知识,可以在本站进行查询。
本文标签: