小编Amy*_*yth的帖子

PHP - Wordpress - 插件小部件更新功能 - 更新数组值[Foreach循环不工作]

我正在开发一个带有小部件的wordpress插件.目前,小部件的更新功能如下所示.

function update($new, $old){
    $instance = $old;
    //Update Values
    $instance['element-one'] = $new['element-one'];
    $instance['element-two'] = $new['element-two'];
    $instance['element-three'] = $new['element-three'];
    $instance['element-four'] = $new['element-four'];
    //Return New Instance
    return $instance;
Run Code Online (Sandbox Code Playgroud)

这应该是应该的.但我有一长串的元素,为了清洁代码我试图用一个简单的功能实现它们如下:

function update($new, $old){
    $instance = $old;
    //Update Values
    foreach($instance as $k => $v){
        $instance[$k] = $new[$k];
    }
    //Return New Instance
    return $instance;
Run Code Online (Sandbox Code Playgroud)

虽然这似乎不起作用.如果我使用此功能,则不会更新Widget值.所以只是为了测试它是否按照我想要的方式工作......我写了一个工作正常的示例脚本.脚本如下.

$a = array(
  'a' => '1',
  'b' => '2',
  'c' => '3'
);

$b = array(
  'a' => 'A',
  'b' => 'B',
  'c' => 'C'
);

function swap_values($old, $new){
  $result …
Run Code Online (Sandbox Code Playgroud)

php foreach function wordpress-plugin

3
推荐指数
1
解决办法
2584
查看次数

Django CreateView:将ForeignKey附加到CustomForm数据

我有一个CreateView如下:

class ResumeCreateView(CreateView):
    model = Resume

    def form_valid(self, request, form):
        candidate = Candidate.objects.get(user=self.request.user)
        self.object = form.save(commit=False)
        self.object.candidate = candidate
        self.object.save()
        f = self.request.FILES.get('file')
        data = [{
            'title': self.request['title'],
            'name': f.name,
        }]  
        response = JSONResponse(data, {}, response_mimetype(self.request))
        response['Content-Disposition'] = 'inline; filename=files.json'
        return response
Run Code Online (Sandbox Code Playgroud)

在这里,我试图将追加candidate实例的Resume模型候选项目这是一个ForeignKeyCandidate模型.

但我总是收到验证错误 {'candidate' : 'This field is required'}

  • 我使用自定义表单而不是模型表单,因为它使用twitter引导标识符.

我错过了什么?

python django foreign-keys form-data create-view

3
推荐指数
1
解决办法
3480
查看次数

在搜索查询集上构造多个字段

如何选择多个字段进行分面SearchQuerySet文档中的示例显示了在单个字段上的方面.

sqs = SearchQuerySet().facet('author')

我说,我有我想刻面像多个字段,author,location,score?我该怎么办?

目前,如果我使用文档中的上述示例,它可以按预期工作,但是如何实现多个字段的分面SearchQuerySet

django field facet django-haystack

3
推荐指数
1
解决办法
1316
查看次数

从中获取特定对象和没有循环的数组的最佳方法

在我的角度应用程序的一个控制器中,我有一个变量集如下.

SomeService.get({}, function(data){
    // this sets xyz as the list of the data retrieved 
    // from the resource within the controllers scope
    $scope.xyz = data.objects;    
});
Run Code Online (Sandbox Code Playgroud)

现在$scope.xyz看起来像

[
    0: {id: 1, ...more data here ...},
    1: {id: 2, ...more data here ...},
    2: {id: 3, ...more data here ...},
    3: {id: 4, ...more data here ...},
    4: {id: 5, ...more data here ...},
    5: {id: 6, ...more data here ...},
]
Run Code Online (Sandbox Code Playgroud)

我想要做的是使用id属性(而不是列表索引)在xyz中获取一个对象.我知道我可以按如下方式迭代数组.

angular.forEach($scope.xyz, function(obj){ return obj.id …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs

3
推荐指数
2
解决办法
8553
查看次数

Python:smtplib sendmail函数

我有一个激活邮件发送脚本如下.

#!/usr/bin/python

__author__ = 'Amyth Arora (***@gmail.com)'

import smtplib
import string
import sys
import random

from email.MIMEText import MIMEText


def generate_activation_key(size=64, chars=string.ascii_lowercase + string.digits):
  return ''.join(random.choice(chars) for x in range(size))


def generate_activation_url(key):
  return 'http://www.example.com/users/activate/' + key

def Activate(name, to_addr):
  sender, reciever = 'mymail@gmail.com', to_addr
  act_url = generate_activation_url(generate_activation_key())
  main_mssg = """
  Dear %s,

  Thakyou for creating an account with Example.com. You are now just one click away from using your example account. Please click the following link to verify this email …
Run Code Online (Sandbox Code Playgroud)

python google-app-engine sendmail smtplib

2
推荐指数
1
解决办法
2404
查看次数

如何在ModelForm中的自定义表单字段中预填充值

假设我的模型如下.

models.py

class Profile(models.Model):
    user = models.OneToOneField(User)
    middle_name = models.CharField(max_length=30, blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)

email在ModelForm中有一个自定义字段如下

forms.py

class ProfileForm(ModelForm):
    email = forms.CharField()
    class Meta:
         model = models.Profile

    fields = ('email', 'middle_name')
Run Code Online (Sandbox Code Playgroud)

在am中设置上述模型的实例,以便在编辑模板的表单中预填充数据,如下所示.

views.py

def edit_profile(request):
    profile = models.Profile.objects.get(user=request.user)
    profileform = forms.ProfileForm(instance=profile)
    return render_to_response('edit.html', { 'form' : 'profileform' }, context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)

现在在表单中,我获得了为Profile模型下的所有字段预填充的所有值,但自定义字段为空,这是有意义的.

但有没有办法可以预先填充自定义字段的值?也许是这样的:

email = forms.CharField(value = models.Profile.user.email)
Run Code Online (Sandbox Code Playgroud)

python django modelform custom-fields

2
推荐指数
1
解决办法
1731
查看次数

检查json字典是否将空字符串作为值

我想要实现的是仅在JSON对象中的元素值等于空字符串时才显示和元素''.

例如:

说我有以下json对象

{
    'id' : 23,
    'name' : 'Adrian Reese',
    'age' : '',
    'location' : ''
}
Run Code Online (Sandbox Code Playgroud)

现在在部分模板中,我显示用户信息,我做了类似的事情:

<h1>{{ user.name | capitalize }}</h1>
<span class="age">Age: {{ user.age }}</span>
<span class="location">Location: {{ user.location }}</span>
Run Code Online (Sandbox Code Playgroud)

对于每个用户,我希望<span>'s只有在值不等于时才能看到''.我怎样才能实现这一目标?

string angularjs

2
推荐指数
1
解决办法
2万
查看次数

解析django测试结果

我最近为我的 django 项目之一编写了一些测试。我现在想做的是从脚本调用测试命令。

我希望解析测试结果并保存它们。这对于 django 测试框架来说是可能的吗?

testing django parsing

2
推荐指数
1
解决办法
881
查看次数

PHP将字符串转换为数组

我希望将以下字符串转换为PHP数组:

{ 'Codes': ['01239EEF', '01240EDF'], 'Done' : ['1', '0'] }
Run Code Online (Sandbox Code Playgroud)

我试图将其转换为类似于以下内容的数组:

{[Codes] => {[0] => '01239EEF', [1] => '01240EDF'}, [Done] => {[0] => '1', [1] => '0'}}
Run Code Online (Sandbox Code Playgroud)

我尝试使用json_decode显式设置的Array参数true.但它总是NULL因某种原因返回.

php arrays json

2
推荐指数
1
解决办法
93
查看次数

发出发布请求引发nullpointerexception

我试图从我的一个android应用程序发出发布请求,但是由于HttpClient.execute某种原因该方法出错。

在我的应用程序中,我有一个makePostRequest方法,用于发出发布请求。方法如下:

// Makes a post request to the server
public void makePostRequest(String url, ArrayList<NameValuePair> postData) {
    HttpClient client = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost();

    try {
        boolean prnull = postRequest == null;
        Log.d("DEBUG:", String.valueOf(prnull));
        postRequest.setEntity(new UrlEncodedFormEntity(postData));
        HttpResponse response = client.execute(postRequest);
        Log.i("INFO:", response.toString());
    } catch (ClientProtocolException e) {
        Log.d("DEBUG:", e.toString());
    } catch (IOException e) {
        Log.d("DEBUG:", e.toString());
    }

}
Run Code Online (Sandbox Code Playgroud)

在活动中,我将其用作:

    // Process user registration
    Button registerButton = (Button) findViewById(R.id.registerButton);
    registerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View …
Run Code Online (Sandbox Code Playgroud)

java post android http

2
推荐指数
1
解决办法
1020
查看次数