我有一个表单,我可以将标题保存为空白,在这种情况下,该函数将空白转换为未命名+主键。但就我而言,保存为空白仍为空白。我不知道这是什么问题。
模板:
<div class="page-header">
<h1>Backtesting{% if form.instance.pk %}: {{form.instance.title}} {% endif %}</div>
<div id="tab1" class="tab" >
<form action="{% url "backtest" %}" method='POST' role='form' id='form'>
{% csrf_token %}
<input type="hidden" name="tabs" value="first" id="toggle-tab1" checked="checked" />
{% include 'tags/parameters_form.html' %}
<br />
{% include 'tags/parameters_backtest_form.html' %}
<br />
{% if user.is_authenticated %}
<input type='submit' id='run' value='Run' class='btn btn-default'>
{% if user.profile.is_active %}
Name: {{ form.title }} <input type='submit' name='save' value='Save' class='btn btn-default'>
{% else %}
<p>
Expired account! you need to reactivate in order to save parameters.
</p>
{% endif %}
{% else %}
Please <a href="{% url 'auth_login' %}">login</a> in order to Run backtesting!
</br>
Our system needs your email in order to notify you once one or more of your simulations are done. This is a safer way for you to keep track of your previous simulations (/jobs).
{% endif %}
models.py:
title = models.CharField('title', max_length=100, default='', blank=True, help_text='Use an indicative name, related to the chosen parameters')
def __unicode__(self):
if self.title:
return u'%s' % (self.title)
else:
return u'Unnamed%s' % (self.pk)
答案 0 :(得分:1)
因为pk是整数。
您需要将其格式化为整数。
return u'Unnamed%d' % (self.pk)