小编aru*_*lmr的帖子

Django South没有为user_profiles创建表

我想重置我的Django项目的数据库,所以我做了以下步骤:

  1. 删除了我的SQLite数据库
  2. 难道 python manage.py syncdb
  3. 难道 python manage.py migrate users --fake

在我创建一个新帐户并登录后,我收到以下错误消息:

no such table: users_userprofile
Run Code Online (Sandbox Code Playgroud)

这是我的用户model.py看起来像:

class UserProfile(models.Model):
  user = models.OneToOneField(User)
  joined_goals = models.ManyToManyField(Goal, related_name="joined_goals")
  followingGoals = models.ManyToManyField(Goal, related_name="following_goals")

  def __unicode__(self):
    return self.user.username

  def get_goals(self):
    try:
      goals = Goal.objects.filter(user=self.user)
      return goals
    except Goal.DoesNotExist:
      return []

def create_user_profile(sender, instance, created, **kwargs):
    if created:
      userProfile = UserProfile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)
Run Code Online (Sandbox Code Playgroud)

因此,有一个UserProfile类,但South似乎没有使表保存用户配置文件.当我这样做时python manage.py schemamigration users --auto,它说似乎没有任何改变.如何创建userprofiles表?

python django django-south

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

拆包浮标时,"解包需要长度为4的字符串参数"?

我正在尝试使用以下方法将十六进制值转换为float(Python 2.7):

def hex2float(x):
    y = 0
    z = x.decode('hex') 
    try:
        y = struct.unpack('!f', z)[0]
    except:
        print sys.exc_info()[1]    
    print 'z = ' + z 
    print 'y = %s' % (y) 
    print 'x = ' + x
    return

def foo28():
    x = '615885'   #8.9398e-039
    hex2float(x)
Run Code Online (Sandbox Code Playgroud)

输出如下:

unpack requires a string argument of length 4
z = aXà
y = 0
x = 615885
Run Code Online (Sandbox Code Playgroud)

我注意到我得到了非常小的值的异常消息.在这种情况下,是否有正确的方法将十六进制值转换为浮点值.

python hex css-float

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

django startapp管理文件丢失

我一直在关注DjangoProject教程.当我python manage.py startapp newapp 在同一目录中运行 时manage.py.在NEWAPP目录中我看到init.py,models.py,tests.py,和views.py而不是admin.py文件.在哪里admin.py

django

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

Django Admin:根据以前的字段值填充字段

我在django admin中有一个模型如下

ChoiceA= (
      ("on-false","on-false"),
       ("on-true","on-true"),
     )

ChoiceB =  (
        ("always","always"),
        ("never","never"),
       )
   id = models.CharField(verbose_name="Field",max_length=32)
   type = models.CharField(verbose_name="Expression",max_length=32)
   action = models.CharField(max_length=32, choices=x)
Run Code Online (Sandbox Code Playgroud)

现在根据用户输入的类型,即如果用户输入type ="a",则应将操作选项设置为ChoiceA,如果用户输入type ="b",则应将操作选项设置为ChoiceB.如何在Django Admin中实现这一目标?

编辑:

action_change.js

jQuery(document).ready(function(){
$("#id_type").change( function(event) {
$.ajax({
        "type"     : "POST",
        "url"      : "/action_choices/",
        "dataType" : "json",
        "cache"    : false,
        "error"   :  alert("hello"),  
        "success"  : function(json) {
            $('#id_action >option').remove();
            for(var j = 0; j < json.length; j++){
                $('#id_action').append($('<option></option>').val(json[j][0]).html(json[j][1]));
            }
        }

});
});
});
Run Code Online (Sandbox Code Playgroud)

django django-admin

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

在MySQL表中设置唯一的列对

我正在使用php-MySQL在后端进行项目.它有各种文章可供浏览,分类为各种类别用户登录系统.
我最近添加了一个监视列表功能,使用户可以将文章添加到他们的监视列表/仪表板中.
我有以下表格.

文章
article-id(int)主要 - 唯一
cat(varchar)
名称(varchar)
subCat(varchar)
description(varchar)
添加日期

用户
用户名(varchar)主要唯一
lname
fname
加入日期

.监视列表
article-id(int)
username(varchar)
date_watch

我们可以看到没有用于监视列表表的唯一键
我想要(username + article-id)一个唯一的对因为每个用户名都存在多于1个文章ID而且反之亦然
我只想插入删除行和它并不需要更新他们
如何防止重复条目?
到现在为止我一直用php检查行数

"SELECT * FROM watchlist WHERE article-id={$id} AND username={$user}"
Run Code Online (Sandbox Code Playgroud)

而如果

mysql_num_rows() >1(not allowed to insert) 
Run Code Online (Sandbox Code Playgroud)

这工作正常,但如果错误地执行INSERT命令将是一个重复的条目
如何防止它?
我正在使用phpmyadmin

php mysql

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

运行django-cms演示页时出错

我尝试了所有的程序安装的Django-CMS,之后,当我尝试运行演示页我提示以下错误.

(djvenv2)shan@shan:~/workspace/projects/djvenv$ pip freeze
Django==1.6.2
PIL==1.1.7
Pillow==2.4.0
South==0.8.4
argparse==1.2.1
dj-database-url==0.3.0
django-classy-tags==0.5.1
django-cms==3.0
django-mptt==0.6.0
django-sekizai==0.7
djangocms-admin-style==0.2.2
djangocms-installer==0.4.1
html5lib==0.999
six==1.6.1
wsgiref==0.1.2

(djvenv2)shan@shan:~/workspace/projects/djvenv$ djangocms -p . my_demo
Database configuration (in URL format) [default sqlite://localhost/project.db]:
django CMS version (choices: 2.4, 3.0, stable, develop) [default stable]:
Django version (choices: 1.4, 1.5, 1.6, stable) [default 1.5]:
Activate Django I18N / L10N setting (choices: yes, no) [default yes]:
Install and configure reversion support (choices: yes, no) [default yes]:
Languages to enable. Option can be provided multiple times, …
Run Code Online (Sandbox Code Playgroud)

django pip django-cms ubuntu-12.04 pillow

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

哪个在python中更好?

我在python中使用了一些条件语句,它们给出了相同的结果.我想知道哪个更好,它们之间的性能和逻辑有什么不同.

情况1:

if a and b and c:
    #some action
Run Code Online (Sandbox Code Playgroud)

VS

if all( (a, b, c) ):
    #some action
Run Code Online (Sandbox Code Playgroud)

案例2:

if a or b or c:
    #some action
Run Code Online (Sandbox Code Playgroud)

VS

if any( (a, b, c) ):
    #some action
Run Code Online (Sandbox Code Playgroud)

案例3:

if not x in a:
    #some action
Run Code Online (Sandbox Code Playgroud)

VS

if x not in a:
    #some action
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,我想知道性能和逻辑的差异以及首选方式.

python conditional-statements

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

facebook登录android LoginButton.setFragment

我有LogginButton的问题:

LoginButton authButton = (LoginButton) v.findViewById(R.id.authButton);
authButton.setFragment(this);
Run Code Online (Sandbox Code Playgroud)

我使用的片段不是来自支持者. .setFragment从支持中获取片段.

怎么解决?(不要从支持更改为片段)

android facebook

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

如何为以编程方式创建的按钮添加 onclick 函数

我有一个表,其中单击按钮时使用 javascript 添加行。这是我的代码:

$("#addToDisplay").click(function (event) {
    $("#tblProducts").removeClass("hide");
    var counter = 1;
    event.preventDefault();
    counter++;
    var newRow = "<tr><td>" + $("#txtProducts").val() + "</td><td>" + ("#txtQty").val()  "</td><td><input type='button' value='Remove' id='btnRemove' /></td></tr>";
    $("#tblProducts").append(newRow);
});
Run Code Online (Sandbox Code Playgroud)

我的问题是,因为我在每行添加删除按钮,因此将其包含在变量 newRow 中,如何为其添加 onClick 事件,以便如果我单击删除按钮,相应的行将被删除?

html jquery

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

如何调用jQuery函数onclick?

当我点击提交按钮时,我正在使用此代码将页面的当前网址添加到div中.我没有得到如何调用函数onclick.这该怎么做?

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>localhost</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
        <script type="text/javascript">
            $(function () {
                var url = $(location).attr('href');
                $('#spn_url').html('<strong>' + url + '</strong>');
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div id="spn_url"></div>
            <input type="submit" value="submit" name="submit">
        </form>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

html javascript jquery

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