我想重置我的Django项目的数据库,所以我做了以下步骤:
python manage.py syncdbpython 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表?
我正在尝试使用以下方法将十六进制值转换为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)
我注意到我得到了非常小的值的异常消息.在这种情况下,是否有正确的方法将十六进制值转换为浮点值.
我一直在关注DjangoProject教程.当我python manage.py startapp newapp
在同一目录中运行
时manage.py.在NEWAPP目录中我看到init.py,models.py,tests.py,和views.py而不是admin.py文件.在哪里admin.py?
我在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) 我正在使用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
我尝试了所有的程序安装的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) 我在python中使用了一些条件语句,它们给出了相同的结果.我想知道哪个更好,它们之间的性能和逻辑有什么不同.
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)
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)
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)
在上面的例子中,我想知道性能和逻辑的差异以及首选方式.
我有LogginButton的问题:
LoginButton authButton = (LoginButton) v.findViewById(R.id.authButton);
authButton.setFragment(this);
Run Code Online (Sandbox Code Playgroud)
我使用的片段不是来自支持者.
.setFragment从支持中获取片段.
怎么解决?(不要从支持更改为片段)
我有一个表,其中单击按钮时使用 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 事件,以便如果我单击删除按钮,相应的行将被删除?
当我点击提交按钮时,我正在使用此代码将页面的当前网址添加到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) django ×4
python ×3
html ×2
jquery ×2
android ×1
css-float ×1
django-admin ×1
django-cms ×1
django-south ×1
facebook ×1
hex ×1
javascript ×1
mysql ×1
php ×1
pillow ×1
pip ×1
ubuntu-12.04 ×1