小编Pi *_*ort的帖子

django错误:'unicode'对象不可调用

我试图从django网站上做django教程,我遇到了一个问题:我必须将我的__unicode__方法添加到我的模型类中,但是当我尝试返回该模型的对象时,我得到以下错误:

in __unicode__
    return self.question()
TypeError: 'unicode' object is not callable
Run Code Online (Sandbox Code Playgroud)

我是相当新的python和django的新手,我真的不知道我错过了什么,如果有人可以指出它id非常感激.一点代码:

我的models.py:

# The code is straightforward. Each model is represented by a class that subclasses django.db.models.Model. Each model has a number of 
# class variables, each of which represents a database field in the model.

from django.db import models

    class Poll(models.Model):
        question = models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published') 

        def __unicode__(self):
            return self.question


    class Choice(models.Model):
        poll = models.ForeignKey(Poll) 
        choice = models.CharField(max_length=200) 
        votes = models.IntegerField()

        def __unicode__(self):
            return …
Run Code Online (Sandbox Code Playgroud)

python django

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

HTML5验证 - PHP表单操作属性为空

我只是在W3C上使用表单(PHP)来验证我的一些html5页面,并得到以下验证错误:

错误:元素表单上属性操作的值不正确:必须为非空.

我认为当表单提交给自己时,最好将action属性留空.这不是这种情况吗?我怎样才能解决这个问题?

谢谢!

forms validation html5

8
推荐指数
2
解决办法
9426
查看次数

Android:屏幕旋转时的片段生命周期

我知道http://developer.android.com/guide/components/fragments.html的图2 我想知道当我旋转屏幕并最终回到"Fragment Active"时,"Fragment Active"会发生什么.

我的问题的背景是我有一个应用程序,无论我是以纵向还是横向模式启动它都能正常工作.但是在屏幕旋转时它会转储

Fragment com.bla.bla did not create a view.
Run Code Online (Sandbox Code Playgroud)

这个片段基本上只有onCreateView实现,没有别的

public View onCreateView(LayoutInflater i, ViewGroup c, Bundle s)
{
   return i.inflate(R.layout.mylayout, c, false);
}
Run Code Online (Sandbox Code Playgroud)

知道屏幕旋转到底发生了什么,我希望能解决这个问题......

编辑:

我尝试了评论者提出的建议,以及更多相关信息.所以他们基本上都建议有一个空的活动布局,如果我看到正确的话,以编程方式添加片段.我有一个纵向的main.xml和一个用于横向的main.xml,现在看起来非常相似(差异是水平与垂直):

main.xml中:

<LinearLayout xmlns:android="http:// and so on" 
    android:layout_width="fill_parent" 
    android:layout_heigt="wrap_content" 
    android:orientation=vertical" 
    android:id="@+id/myContainer">
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我的活动的onCreate方法如下所示:

super.onCreate(savedInstanceBundle);
setContentView(R.layout.main);

Fragment1 f1 = newFragment1();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.myContainer, f1);
//and I need a second fragment
Fragment2 f2 = newFragment2();
ft.add(R.id.myContainer, f2);
ft.commit();
Run Code Online (Sandbox Code Playgroud)

屏幕旋转似乎与此一致(所以到目前为止,谢谢你!)但是在风景中我只看到第一个片段在纵向中我看到了两个,第二个多次(我更频繁地旋转,它们被添加的次数越多).所以要么我有布局问题,要么我不能添加这样的多个片段.仍然试图弄清楚它是否是一个布局问题,但还没有任何线索.任何提示?

android android-fragments

8
推荐指数
1
解决办法
9644
查看次数

无法在ListFragment上使用SimpleAdapter

我正在Android片段中开发一个ListView.该类扩展了ListFragment.

我试过这个例子:http: //www.heikkitoivonen.net/blog/2009/02/15/multicolumn-listview-in-android/

但问题是,如果类扩展了ListFragment,则没有定义构造函数SimpleAdapter,将其更改为ListActivity会使SimpleAdapter工作,但是应用程序不会.

这是代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View tmp_view = inflater.inflate(R.layout.clients_list, container, false);
    ListView list = (ListView) tmp_view.findViewById(R.id.list);

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("train", "101");
    map.put("from", "6:30 AM");
    map.put("to", "7:40 AM");
    mylist.add(map);
    map = new HashMap<String, String>();
    map.put("train", "103(x)");
    map.put("from", "6:35 AM");
    map.put("to", "7:45 AM");
    mylist.add(map);
    // ...
    SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, …
Run Code Online (Sandbox Code Playgroud)

android listview fragment simpleadapter

4
推荐指数
1
解决办法
7036
查看次数

多态数据类型的函数

数据Foo a定义如下:

data Foo a where
  Foo :: (Typeable a, Show a) => a -> Foo a
  -- perhaps more constructors

instance Show a => Show (Foo a) where
  show (Foo a) = show a
Run Code Online (Sandbox Code Playgroud)

有些情况:

fiveFoo :: Foo Int
fiveFoo = Foo 5

falseFoo :: Foo Bool
falseFoo = Foo False
Run Code Online (Sandbox Code Playgroud)

我如何定义任何函数b -> Foo a,例如:

getFoo :: (Show a, Typeable a) => String -> Foo a
getFoo "five" = fiveFoo
getFoo "false" = falseFoo
Run Code Online (Sandbox Code Playgroud)

这里 …

polymorphism haskell existential-type gadt

4
推荐指数
1
解决办法
479
查看次数

为什么加密哈希函数中的冲突检测会使查找其他冲突更容易?

从维基百科,我读到:

Joux [3]指出2次碰撞会导致n次碰撞:如果找到具有相同MD5散列的两条消息是可行的,那么找到与攻击者希望具有相同MD5哈希值一样多的消息实际上并不困难.

但为什么会这样呢?我无法想象为什么?算法是开放的,人们可以读取产生哈希的数学,这是摘要机制.因此,如果我们知道一次碰撞,为什么它有助于找到新的?

它只是对两个第一个碰撞消息进行小的迭代,然后监视它们的更改以重新映射它们吗?

security hash cryptography digest-authentication

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

为什么在Django模型中需要这个方法?

class mytable(models.Model):
    abc = ...
    xyz = ...
    def __unicode__(self):
Run Code Online (Sandbox Code Playgroud)

为什么有def __unicode__必要?

python django unicode

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

使用元组和operator.itemgetter对字典进行排序

我在最后一个里程碑之前准备合适的输出.我认为它非常简单,但我的方法不起作用:

给出一本字典

mydict = {x1: (val1, dist1, (lat1, lon1)), x2: (val2, dist2, (lat2, lon2)), ...}
Run Code Online (Sandbox Code Playgroud)

我尝试在数组中以嵌套方式对其进行排序,首先使用"值",如果等于,则排序为"dist".

但是,我做了我的作业并尝试使用这种方式:

import operator
s = sorted(mydict.iteritems(), key=operator.itemgetter(1))
Run Code Online (Sandbox Code Playgroud)

问题是,如果排序方法适用于元组,那将非常简单,因为它已经是正确的顺序.

不幸的是我得到:

'list'对象不可调用

你有好主意吗?

python numpy

0
推荐指数
1
解决办法
387
查看次数