tastypie 发布和完整示例
是否有完整的 tastypie django 示例站点和设置可供下载?我一整天都在努力解决这个问题。我有以下代码。基本上,我有一个使用 ajax 处理的 POST 表单。当我单击表单上的“提交”并且运行 ajax 请求时,调用返回“POST http: //192.168.1.110:8000/api/private/client_basic_info/ 404(未找到)”我我认为 URL 配置正确。我可以访问 http://192.168.1.110:8000/api/private/client_basic_info /?format=json 就可以了。我是否遗漏了一些设置或在我的方法中犯了一些基本错误?我的意图是每个用户只能填写/修改一张且只有一张“客户基本信息”表格/模型。
页面:
{% extends "layout-column-100.html" %}
{% load uni_form_tags sekizai_tags %}
{% block title %}Basic Information{% endblock %}
{% block main_content %}
{% addtoblock "js" %}
<script language="JavaScript">
$(document).ready( function() {
$('#client_basic_info_form').submit(function (e) {
form = $(this)
form.find('span.error-message, span.success-message').remove()
form.find('.invalid').removeClass('invalid')
form.find('input[type="submit"]').attr('disabled', 'disabled')
e.preventDefault();
var values = {}
$.each($(this).serializeArray(), function(i, field) {
values[field.name] = field.value;
})
$.ajax({
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(values),
dataType: 'json',
processData: false,
url: '/api/private/client_basic_info/',
success: function(data, status, jqXHR) {
form.find('input[type="submit"]')
.after('<span class="success-message">Saved successfully!</span>')
.removeAttr('disabled')
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR)
console.log(textStatus)
console.log(errorThrown)
var errors = JSON.parse(jqXHR.responseText)
for (field in errors) {
var field_error = errors[field][0]
$('#id_' + field).addClass('invalid')
.after('<span class="error-message">'+ field_error +'</span>')
}
form.find('input[type="submit"]').removeAttr('disabled')
}
}) // end $.ajax()
}) // end $('#client_basic_info_form').submit()
}) // end $(document).ready()
</script>
{% endaddtoblock %}
{% uni_form form form.helper %}
{% endblock %}
资源
from residence.models import ClientBasicInfo
from residence.forms.profiler import ClientBasicInfoForm
from tastypie import fields
from tastypie.resources import ModelResource
from tastypie.authentication import BasicAuthentication
from tastypie.authorization import DjangoAuthorization, Authorization
from tastypie.validation import FormValidation
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
resource_name = 'user'
fields = ['username']
filtering = {
'username': ALL,
}
include_resource_uri = False
authentication = BasicAuthentication()
authorization = DjangoAuthorization()
def dehydrate(self, bundle):
forms_incomplete = []
if ClientBasicInfo.objects.filter(user=bundle.request.user).count() < 1:
forms_incomplete.append({'name': 'Basic Information', 'url': reverse('client_basic_info')})
bundle.data['forms_incomplete'] = forms_incomplete
return bundle
class ClientBasicInfoResource(ModelResource):
user = fields.ForeignKey(UserResource, 'user')
class Meta:
authentication = BasicAuthentication()
authorization = DjangoAuthorization()
include_resource_uri = False
queryset = ClientBasicInfo.objects.all()
resource_name = 'client_basic_info'
validation = FormValidation(form_class=ClientBasicInfoForm)
list_allowed_methods = ['get', 'post', ]
detail_allowed_methods = ['get', 'post', 'put', 'delete']
编辑:
我的资源文件现在是:
from residence.models import ClientBasicInfo
from residence.forms.profiler import ClientBasicInfoForm
from tastypie import fields
from tastypie.resources import ModelResource
from tastypie.authentication import BasicAuthentication
from tastypie.authorization import DjangoAuthorization, Authorization
from tastypie.validation import FormValidation
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
resource_name = 'user'
fields = ['username']
filtering = {
'username': ALL,
}
include_resource_uri = False
authentication = BasicAuthentication()
authorization = DjangoAuthorization()
#def apply_authorization_limits(self, request, object_list):
# return object_list.filter(username=request.user)
def dehydrate(self, bundle):
forms_incomplete = []
if ClientBasicInfo.objects.filter(user=bundle.request.user).count() < 1:
forms_incomplete.append({'name': 'Basic Information', 'url': reverse('client_basic_info')})
bundle.data['forms_incomplete'] = forms_incomplete
return bundle
class ClientBasicInfoResource(ModelResource):
# user = fields.ForeignKey(UserResource, 'user')
class Meta:
authentication = BasicAuthentication()
authorization = DjangoAuthorization()
include_resource_uri = False
queryset = ClientBasicInfo.objects.all()
resource_name = 'client_basic_info'
validation = FormValidation(form_class=ClientBasicInfoForm)
#list_allowed_methods = ['get', 'post', ]
#detail_allowed_methods = ['get', 'post', 'put', 'delete']
def apply_authorization_limits(self, request, object_list):
return object_list.filter(user=request.user)
我将 ClientBasicInfo 的用户字段设置为可为空,并且 POST 似乎可以工作。我现在想尝试更新该条目。那只是将 pk 附加到 ajax url 吗?例如/api/private/client_basic_info/21/?当我提交该表单时,我收到一条 501 NOT IMPLEMENTED 消息。我到底还没有实施什么?我正在子类化 ModelResource,它应该根据文档实现所有与 ORM 相关的函数。
Is there a full tastypie django example site and setup available for download? I have been wrestling with wrapping my head around it all day. I have the following code. Basically, I have a POST form that is handled with ajax. When I click "submit" on my form and the ajax request runs, the call returns "POST http://192.168.1.110:8000/api/private/client_basic_info/ 404 (NOT FOUND)" I have the URL configured alright, I think. I can access http://192.168.1.110:8000/api/private/client_basic_info/?format=json just fine. Am I missing some settings or making some fundamental errors in my methods? My intent is that each user can fill out/modify one and only one "client basic information" form/model.
a page:
{% extends "layout-column-100.html" %}
{% load uni_form_tags sekizai_tags %}
{% block title %}Basic Information{% endblock %}
{% block main_content %}
{% addtoblock "js" %}
<script language="JavaScript">
$(document).ready( function() {
$('#client_basic_info_form').submit(function (e) {
form = $(this)
form.find('span.error-message, span.success-message').remove()
form.find('.invalid').removeClass('invalid')
form.find('input[type="submit"]').attr('disabled', 'disabled')
e.preventDefault();
var values = {}
$.each($(this).serializeArray(), function(i, field) {
values[field.name] = field.value;
})
$.ajax({
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(values),
dataType: 'json',
processData: false,
url: '/api/private/client_basic_info/',
success: function(data, status, jqXHR) {
form.find('input[type="submit"]')
.after('<span class="success-message">Saved successfully!</span>')
.removeAttr('disabled')
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR)
console.log(textStatus)
console.log(errorThrown)
var errors = JSON.parse(jqXHR.responseText)
for (field in errors) {
var field_error = errors[field][0]
$('#id_' + field).addClass('invalid')
.after('<span class="error-message">'+ field_error +'</span>')
}
form.find('input[type="submit"]').removeAttr('disabled')
}
}) // end $.ajax()
}) // end $('#client_basic_info_form').submit()
}) // end $(document).ready()
</script>
{% endaddtoblock %}
{% uni_form form form.helper %}
{% endblock %}
resources
from residence.models import ClientBasicInfo
from residence.forms.profiler import ClientBasicInfoForm
from tastypie import fields
from tastypie.resources import ModelResource
from tastypie.authentication import BasicAuthentication
from tastypie.authorization import DjangoAuthorization, Authorization
from tastypie.validation import FormValidation
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
resource_name = 'user'
fields = ['username']
filtering = {
'username': ALL,
}
include_resource_uri = False
authentication = BasicAuthentication()
authorization = DjangoAuthorization()
def dehydrate(self, bundle):
forms_incomplete = []
if ClientBasicInfo.objects.filter(user=bundle.request.user).count() < 1:
forms_incomplete.append({'name': 'Basic Information', 'url': reverse('client_basic_info')})
bundle.data['forms_incomplete'] = forms_incomplete
return bundle
class ClientBasicInfoResource(ModelResource):
user = fields.ForeignKey(UserResource, 'user')
class Meta:
authentication = BasicAuthentication()
authorization = DjangoAuthorization()
include_resource_uri = False
queryset = ClientBasicInfo.objects.all()
resource_name = 'client_basic_info'
validation = FormValidation(form_class=ClientBasicInfoForm)
list_allowed_methods = ['get', 'post', ]
detail_allowed_methods = ['get', 'post', 'put', 'delete']
Edit:
My resources file is now:
from residence.models import ClientBasicInfo
from residence.forms.profiler import ClientBasicInfoForm
from tastypie import fields
from tastypie.resources import ModelResource
from tastypie.authentication import BasicAuthentication
from tastypie.authorization import DjangoAuthorization, Authorization
from tastypie.validation import FormValidation
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
resource_name = 'user'
fields = ['username']
filtering = {
'username': ALL,
}
include_resource_uri = False
authentication = BasicAuthentication()
authorization = DjangoAuthorization()
#def apply_authorization_limits(self, request, object_list):
# return object_list.filter(username=request.user)
def dehydrate(self, bundle):
forms_incomplete = []
if ClientBasicInfo.objects.filter(user=bundle.request.user).count() < 1:
forms_incomplete.append({'name': 'Basic Information', 'url': reverse('client_basic_info')})
bundle.data['forms_incomplete'] = forms_incomplete
return bundle
class ClientBasicInfoResource(ModelResource):
# user = fields.ForeignKey(UserResource, 'user')
class Meta:
authentication = BasicAuthentication()
authorization = DjangoAuthorization()
include_resource_uri = False
queryset = ClientBasicInfo.objects.all()
resource_name = 'client_basic_info'
validation = FormValidation(form_class=ClientBasicInfoForm)
#list_allowed_methods = ['get', 'post', ]
#detail_allowed_methods = ['get', 'post', 'put', 'delete']
def apply_authorization_limits(self, request, object_list):
return object_list.filter(user=request.user)
I made the user field of the ClientBasicInfo nullable and the POST seems to work. I want to try updating the entry now. Would that just be appending the pk to the ajax url? For example /api/private/client_basic_info/21/? When I submit that form I get a 501 NOT IMPLEMENTED message. What exactly haven't I implemented? I am subclassing ModelResource, which should have all the ORM-related functions implemented according to the docs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我明白了。我不小心。 AJAX 请求类型应该是“PUT”来处理 501 未实现错误(我正在执行更新)。我还设置了一个自定义身份验证类来处理 403 错误。
Okay, I figured it out. I wasn't being careful. The AJAX request type should have been "PUT" to handle the 501 not implemented error (I was performing an update). I have also set up a custom authentication class to handle 403 errors.