我的型号:
class Transaction (models.Model):
transaction_id = models.AutoField(primary_key=True)
net_monthly_transaction = models.DecimalField(max_digits = 10, decimal_places = 2, default=0)
# deposit or withdrawal (withdrawal with negative value)
amount = models.DecimalField(max_digits = 10, decimal_places = 2)
time_stamp = models.DateTimeField(default=datetime.now, blank=True)
def __str__(self): # __unicode__ on Python 2
return str(self.time_stamp) + str(self.amount) + str(self.net_monthly_transaction)
Run Code Online (Sandbox Code Playgroud)
我的目标是从每个月的最后一个条目中获取 net_monthly_transaction 的值。
在SO的帮助下我已经做到了这一点:
truncate_date = connection.ops.date_trunc_sql('month', 'time_stamp')
lem = Transaction.objects.extra({'month':truncate_date}).values('month').annotate(last_record=Max('time_stamp')).values_list('net_monthly_transaction', flat=True)
Run Code Online (Sandbox Code Playgroud)
上面的查询假设从每个月的最大 time_stamp 中获取 net_monthly_transaction 的值。
但事实并非如此。
如果我为 10 月份一个接一个地创建三个条目:
查询将返回所有 …
我有一个文本文件,每行都有关键字,如下所示:
foo
foo1
^^^^^^^^^
foo5
foo7
Run Code Online (Sandbox Code Playgroud)
^^^^^^^^^是一个标志,设置为一旦到达就中断 for 循环:
keywords = []
with open("keywords.txt") as f:
for line in f:
if line.startswith(request.GET.get('search', '')):
keywords.append(line.lower())
if line == "^^^^^^^^^":
break
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,第二个条件永远不会满足(**if line == "^^^^^^^^^":**)。
我也尝试is过==(但没想到它能起作用,而且没有)。
当我尝试时line.startswith("^^^^^^"):,满足条件,循环结束。我想知道为什么==在上述情况下不起作用。
寻找一些方向和解释。
我在更改Django Form的CSS时遇到了一些问题。这是我的代码(模板):
<form method="post" action="." id="signup">
<div class="container">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" value="{% trans 'Submit' %}">Join</button>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
模型:
class ExRegistrationForm(RegistrationForm):
age = forms.IntegerField()
location = forms.CharField()
phone = forms.CharField()
Run Code Online (Sandbox Code Playgroud)
模型:
class ExUserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
age = models.PositiveSmallIntegerField(default = 19)
location = models.CharField(max_length=2, default = "location")
phone = models.CharField(max_length=15, default = "phone number")
def __unicode__(self):
return self.user
def user_registered_callback(sender, user, request, **kwargs):
profile = ExUserProfile(user = user)
profile.age = request.POST["age"]
profile.location = request.POST["location"]
profile.phone …Run Code Online (Sandbox Code Playgroud) 我正在尝试生成0到100之间的四个随机数,它们将等于100.
我已设法产生结果,但效率不高.我的方法只是在0-100之间循环随机数,然后添加它们,如果它不等于100然后重复该过程,直到它等于100.是否有更有效的方法?
提前致谢
(我正在使用Django 1.3 - Python 2.7)
经过所有相关问题,没有任何解决方案.
语法错误在第5行,而不是第1行:
{% load static %}
<html>
<head>
<link rel="stylesheet" href="{% static 'css/home.css' %}">
Run Code Online (Sandbox Code Playgroud)
在审核相关问题后,一些建议的第一行应改为:
{% load staticfiles %}
Run Code Online (Sandbox Code Playgroud)
我试过,而不是"无效的块标记:'静态'"错误,我得到:
'staticfiles'不是有效的标记库:找不到模板库staticfiles,尝试过django.templatetags.staticfiles,django.contrib.admin.templatetags.staticfiles
我仔细检查了设置文件:
STATIC_ROOT = '/home/username/mysite/static'
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'mysite.myapp', …Run Code Online (Sandbox Code Playgroud) 我在视图中返回多个值时遇到了一些困难.我可以创建一个模型,将值存储在模型中,然后返回它,但这种方法不是我想要的,因为这些值只能使用一次.
是否可以返回多个值而不将它们存储在模型中?
例如,view.py:
loanAmount = request.GET.get('desired_loan')
repaymentTime = request.GET.get('repayment_time')
return render(request, 'blog/draw.html', "I want to return loanAmount and repaymentTime")
Run Code Online (Sandbox Code Playgroud)
然后在模板中,简单地说:
{{ loanAmount }}
Run Code Online (Sandbox Code Playgroud)
我该怎么做?任何方向或帮助将不胜感激.
谢谢,
我在我的一个模型中添加了一个用户外部字段并进行了迁移。系统提示我添加默认值。user 的默认值假设为 Int,表示 user_id。我没有添加 Int,而是添加了字符串“username”。
现在我被困住了:
return int(value)
ValueError: invalid literal for int() with base 10: 'testuser'
Run Code Online (Sandbox Code Playgroud)
我删除了与模型关联的所有对象,并尝试迁移,但这不起作用,出现相同的错误。( Model.objects.all().delete())
我恢复到以前的迁移,这并没有解决问题,当我迁移时,它得到了相同的 ValueError。( ./manage.py migrate app 0037_auto_20170330_0326)
最后,我删除了新的外国字段,但这也没有解决问题。
在这种情况下我还有什么其他选择?
我的模型非常简单:
class Chat(models.Model):
user = models.ForeignKey(User) #new field that was added
message = models.CharField(max_length=50)
Run Code Online (Sandbox Code Playgroud) django ×5
python ×3
addition ×1
css ×1
django-forms ×1
html ×1
java ×1
line-by-line ×1
mysql ×1
random ×1
text-files ×1