我是Django的新手,但我正在建立一个人们可以就某些有争议的主题发表声明的网站.现在我只是为每次辩论手动添加了一些语句,我希望用户能够对一个语句进行"投票".
这是我现在的代码:
来自models.py的代码
class Debate(models.Model):
title = models.CharField(max_length=50)
description = models.CharField(max_length=100)
pub_date = models.DateTimeField('Date Published')
url_slug = models.SlugField(editable=False)
def __unicode__(self):
return self.title
class Statement(models.Model):
statement = models.CharField(max_length=200)
debate = models.ForeignKey(Debate)
votes = models.IntegerField(default=0)
user = models.ForeignKey(User)
def __unicode__(self):
return self.statement
Run Code Online (Sandbox Code Playgroud)
来自views.py的代码
def debate_page(request, url_slug):
if request.method == 'POST':
for statement in statements:
statement.votes = statement.votes+1
debate = Debate.objects.get(url_slug=url_slug)
template = get_template('debate_page.html')
statements = Statement.objects.filter(debate=debate)
variables = Context ({
'debate_title': debate.title,
'debate_description': debate.description,
'statements': statements,
})
output = template.render(variables)
return HttpResponse(output) …Run Code Online (Sandbox Code Playgroud) 所以我有一项任务,我必须使用Tkinter来创建棋盘游戏.这只是我希望引入电路板图像的程序的一部分.但我继续得到错误,"太早创建图像",我不知道我做错了什么.
到目前为止,这是我的代码:
from Tkinter import *
from pprint import pprint
# Which variable is currently updating
from variableColors import Variables
VariableIndex = 0
VariableText = Variables[VariableIndex]
Ids = None # Current canvas ids of the text and 4 player tokens:
# Will be None if not committed
VariableCoords = { } # Where store variable and coordinates
im = PhotoImage(file="C:\Users\Kiki\Desktop\Assignment\\")
photo = can.create_image(0,0,anchor=NW, image=im)
can.pack()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.谢谢 :)
所以我在Python中有一个包含4个字符串的列表,我希望返回该列表,但是随机化并且只能达到一个特定的数字(下面代码中的变量'players').我不能使用随机播放功能,但如果可以,请相信我,我愿意.
这是我到目前为止的代码:
players = raw_input('How many players? ')
players = int(players)
Roles = ["Role1", "Role2", "Role3", "Role4"]
print Roles[:players]
Run Code Online (Sandbox Code Playgroud)
我需要以某种方式使用random.seed()函数随机化列表.我真的很困惑,因为我认为你只能使用random.seed()和数字,而不是字符串列表.如果有人可以帮助我,我会非常感激.
所以我在Tkinter的画布上有一堆文本,我想让它在鼠标悬停在文本上时文本颜色发生变化.对于我的生活,我无法弄清楚如何做到这一点,似乎没有任何关于Tkinter的信息.
for city in Cities:
CityText = Cities[i]
board.create_text(CityLocs[CityText][0], CityLocs[CityText][1], text=CityText, fill="white")
CityText = Cities[i]
i = i + 1
Run Code Online (Sandbox Code Playgroud)
这只是我将文本放在画布上的代码,虽然我不确定还有什么可以发布我的观点.是否没有'悬停'功能或类似Tkinter内置的功能?
我有一个网站,我正在尝试制作一个可打印的版本,我正在使用一个新的打印样式表,但我遇到了问题.我有一个星级评级的DIV,它只是html文件中的一个空DIV,但在常规CSS中我有实际星星的背景图像.
html文件中的DIV如下所示:
<div class="example_rating"></div>
Run Code Online (Sandbox Code Playgroud)
常规样式表中的代码如下所示:
.example_rating {
height:40px;
width:200px;
margin-left:20%;
background: url('../stars.png') no-repeat scroll 0 0 transparent;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是星级评分在网页的可打印格式中根本没有出现.我没有DIV隐藏或任何东西,但我不知道我能做什么让它以可打印格式显示.
我意识到这个问题可能有点模糊,但我不确定如何解释它.如果需要,我绝对可以提供更多细节.谢谢!
所以我有两个列表,我想使用.pop()从ListA中删除一个项目,然后使用.append()将它添加到ListB.我试过这个,但是一旦我使用.pop(),.append()函数就会占用一个索引.
这是我到目前为止的代码:
ListA = ['a', 'b', 'c', 'd', 'e']
ListB = []
ListA.pop()
ListA.pop()
ListA.pop()
print 'ListA =', ListA
print 'ListB =', ListB
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
ListA = ['a', 'b']
ListB = []
Run Code Online (Sandbox Code Playgroud)
我希望输出看起来像这样:
ListA = ['a', 'b']
ListB = ['e', 'd', 'c']
Run Code Online (Sandbox Code Playgroud)
我知道我没有任何.append()函数,但当我把它们放在那里时我得到一个错误.这就是代码只使用.pop()函数.我想使用.pop()删除正在删除的项目,然后将其附加到ListB.
谢谢你的帮助.
所以我决定用Python编写Monopoly,但是我在更新玩家位置时遇到了一些麻烦.我写了一个for循环,遍历玩家,为每个玩家掷骰子,然后更新他们的位置.问题是位置变量没有保留最新位置,它在for循环开始时保持重置为0.这是我的代码:
player1location = 0
def turn(numberPlayers, player, player1location, Board):
for player in range(numberPlayers):
player = 'Player'+str(player+1)
print 'It\'s', player, 'turn!'
print player1location
rollDice = raw_input('Press Enter to roll the dice!')
diceRoll = random.randint(1,6)
print player, 'rolled a', diceRoll
player1location = player1location + diceRoll
print 'You landed on', player1location
print '\n'
while True:
turn(numberPlayers, player, player1location, Board)
Run Code Online (Sandbox Code Playgroud)
如有必要,我可以提供更多代码,但我认为这是控制玩家位置的所有内容.谢谢!
编辑:显然我正在改变局部变量而不是全局变量.我将如何更改全局变量?
基本上,我有一个列表,我想立即执行多个功能.例如,
List = [1,2,3,4,5]
List.extend([1,2,3,4,5]).sort().reverse()
Run Code Online (Sandbox Code Playgroud)
我希望结果如此[5,5,4,4,3,3,2,2,1,1].
我有一段时间没有使用过Python,但我知道我之前做过类似的事情.这是简单的我缺少什么或什么?
顺便说一句,它必须在一条线上.