我对AngularJS的$ http有一个奇怪的行为,并没有真正理解transformResponse是如何工作的(文档在这一点上有点亮).
WebAssets.get = function () {
return $http.get('/api/webassets/list', {
transformResponse: [function (data, headersGetter) {
// not sure what to do here?!
return data;
}].concat($http.defaults.transformResponse) // presume this isn't needed, added for clarity
}).then(function (response) {
return new WebAssets(response.data);
});
};
Run Code Online (Sandbox Code Playgroud)
api返回一个对象数组:
[{"webasset_name": "...", "application_id": "...", "etc": "..."}, ... ]
Run Code Online (Sandbox Code Playgroud)
但是当transformResponse做了它的恶行时,数据已经变成了一个索引对象:
{"0":{"webasset_name":"...","application_id":"...", "etc": "..."}, "1":....}
Run Code Online (Sandbox Code Playgroud)
我想保留原始数据结构(对象数组).
在localhost:8000上运行django dev服务器,在localhost:3000上运行nodejs服务器.我想将json导入nodejs服务器,但我收到此错误:
XMLHttpRequest无法加载 http://127.0.0.1:8000/api/presentation/?format=json.Access-Control-Allow-Origin不允许使用origin http:// localhost:3000
这是我第一次涉足跨领域的乐趣,所以我很想走出我的深度.
我已将此添加到节点(expressjs)中的路由中.
app.all('/', function(req, res){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.render('index', {
title: '...'
});
});
Run Code Online (Sandbox Code Playgroud)
我错过了什么/做错了什么?
curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"latlong": "test"}' http://localhost:8000/geo/api/geolocation/
Run Code Online (Sandbox Code Playgroud)
以上工作正常,但当我尝试在下面的ajax中复制POST时,我得到500错误.
$.ajax({
type: 'POST',
url: 'http://localhost:8000/geo/api/geolocation/',
data: '{"latlong": "test"}',
success: latlongSaved(),
dataType: "application/json",
processData: false,
});
Run Code Online (Sandbox Code Playgroud)
错误信息是:
{"error_message": "The format indicated 'application/x-www-form-urlencoded' had no available deserialization method. Please check your ``formats`` and ``content_types`` on your Serializer." .... }
Run Code Online (Sandbox Code Playgroud)
值得注意的是这是跨域的,我正在使用通过git:gist找到的django-crossdomainxhr-middleware.py
如果我向ajax调用添加内容类型,如下所示:
contentType: "application/json"
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
XMLHttpRequest cannot load http://localhost:8000/geo/api/geolocation/. Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
Request URL:http://localhost:8000/geo/api/geolocation/
Request Method:OPTIONS
Status Code:200 OK
Request Headersview source
Access-Control-Request-Headers:Origin, Content-Type, …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个具有重复纹理的长走廊.如何添加重复纹理并以直角旋转对象(在本例中为平面)以创建走廊墙和天花板?
var texture, material, plane;
texture = THREE.ImageUtils.loadTexture( "../img/texture.jpg" );
texture.wrapT = THREE.RepeatWrapping; // This doesn't seem to work;
material = new THREE.MeshLambertMaterial({ map : texture });
plane = new THREE.Mesh(new THREE.PlaneGeometry(400, 3500), material);
plane.doubleSided = true;
plane.position.x = 100;
plane.rotation.z = 2; // Not sure what this number represents.
scene.add(plane);
Run Code Online (Sandbox Code Playgroud) inuse当我保存ModelForm时,我想将BooleanField设置为True(我在管理区域之外使用表单),我不确定如何做到这一点.
楷模:
class Location(models.Model):
place = models.CharField(max_length=100)
inuse = models.BooleanField()
class Booking(models.Model):
name = models.CharField(max_length=100, verbose_name="Your name*:")
place = models.ManyToManyField(Location, blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)
形式:
class BookingForm(ModelForm):
class Meta:
model = Booking
def save(self, commit=True):
booking = super(BookingForm, self).save(commit=False)
if commit:
booking.save()
self.save_m2m()
for location in booking.place.all():
location.inuse = True
print location #nothing prints
location.save()
Run Code Online (Sandbox Code Playgroud)
视图:
def booking(request):
form = BookingForm()
if request.method == 'POST':
form = BookingForm(request.POST)
if form.is_valid():
form.save()
else:
form = form
return render_to_response('bookingform.html', {
'form': form,
}) …Run Code Online (Sandbox Code Playgroud) 我有一个后端渲染模板,它返回一个JSON对象,其中包含一个需要一些动态数据绑定的字符串,例如......
sampleLogic = {
"1": "Sample static text and some {{ dynamic_text }}."
}
Run Code Online (Sandbox Code Playgroud)
默认情况下,字符串是转义的,在angular中转换dynamic_text以绑定到$ scope.dynamic_text的最佳方法是什么?
JS:
var sampleLogic = {
"1": "Sample static text and some {{ dynamic_text }}."
};
function parseMe($scope) {
$scope.copy = sampleLogic['1'];
$scope.dynamic_text = "dynamic text woooot";
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<div ng-app>
<div ng-controller="parseMe">
<div ng-bind-html-unsafe="copy"></div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
models.py
class Category(models.Model):
name = models.CharField(max_length=50)
class SubCatergory(models.Model):
parent_category = models.ForeignKey(Category)
name = models.CharField(max_length=100)
Run Code Online (Sandbox Code Playgroud)
views.py
def all_products(request):
c = Category.objects.all()
s = SubCatergory.objects.all()
return render_to_response('all_products.html',
{'c':c, 's':s})
Run Code Online (Sandbox Code Playgroud)
all_products.html
{% for category in c %}
<h1>{{ category.name }}</h1>
<ul>
{% for sub in s %}
{% if category.id == sub.parent_category.id %}
<li>{{ sub.name }}</li>
{% endif %}
{% endfor %}
</ul>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
只是想知道以上是否是外键查询的最佳实践.我在模板级别进行过滤(如果是category.id == sub ...),我应该将其移动到模型或视图级别吗?
顶部边框为6px和绿色.左,右和底1px #ccc.1px边框正在流入6px,请参阅小提琴示例. http://jsfiddle.net/AzHUt/34/
div汤以外的任何解决方案?
请原谅我的新手问题,但如何转换:
[<Location: London>]或[<Location: Edinburgh>, <Location: London>] 等
成:
'伦敦'或'爱丁堡,伦敦'
将其置于上下文中的一些背景信息:
Models.py:
class Location(models.Model):
place = models.CharField(max_length=100)
def __unicode__(self):
return self.place
class LocationForm(ModelForm):
class Meta:
model = Location
Run Code Online (Sandbox Code Playgroud)
forms.py
class BookingForm(forms.Form):
place = forms.ModelMultipleChoiceField(queryset=Location.objects.all(), label='Venue/Location:', required=False)
Run Code Online (Sandbox Code Playgroud)
views.py
def booking(request):
if request.method == 'POST':
form = BookingForm(request.POST)
if form.is_valid():
place = form.cleaned_data['place']
recipients.append(sender)
message = '\nVenue or Location: ' + str(place)
send_mail('Thank you for booking', message, sender, recipients)
)
Run Code Online (Sandbox Code Playgroud) 在内部管理员我想要list_display一个模型包括get_absolute_url和可点击.目前它只是显示/x/(x是ID).任何快速修复?
模型:
def get_absolute_url(self):
return "/%i/" % self.id
Run Code Online (Sandbox Code Playgroud)
管理员:
list_display = ('name', 'get_absolute_url')
Run Code Online (Sandbox Code Playgroud) django ×6
javascript ×3
angularjs ×2
python ×2
ajax ×1
css ×1
django-admin ×1
django-forms ×1
django-views ×1
express ×1
jquery ×1
json ×1
node.js ×1
tastypie ×1
three.js ×1