Django显示变量不起作用

时间:2015-11-02 23:57:37

标签: django django-models django-views

在我的模板中,我有:

 {{ output }}
 {% for event in latest_event_list %}
    <li><a href="/student/order/{{ event.id }}/">{{ event.name }}</a></li>
{% endfor %}

在我的观点中:

def order(request):
        latest_event_list = Events.objects.order_by('-date')[:5]
        #output = ', '.join([str(p.cost) for p in latest_event_list])
        output = {'latest_event_list': latest_event_list}
        print latest_event_list
        print output
    if request.method == 'POST':
        print "POST"
        form = OrderForm(request.POST)
        print form
        else:
        form = OrderForm()  
    return render(request, 'student/orderform.html', {'output': output})

在我的模特中:

class EventOption(models.Model):
    event = models.ForeignKey(Events)
    description = models.CharField(max_length = 500)
    option_text = models.CharField(max_length = 50)
    votes = models.IntegerField(default = 0)
    def __unicode__(self):
    return str(self.option_text)
# Create your models here.

在网站中,{{output}}会显示一些内容,但for循环不会显示任何内容。这有什么理由吗?以及如何解决这个问题?

支持先锋。

1 个答案:

答案 0 :(得分:4)

您创建$.ajax { type: "POST", url: 'my_url/process', //etc etc success: function(data){ //calling my get_employee function now params = get_employee(); //this is how I want to use the returned object as JSON frm = $("<form action='/save/employee/database' method='POST'></form>"); frm.append("<input type='hidden' name='employee_id' value='" + params.employee.id + "'>"); frm.append("<input type='hidden' name='card_id' value='" + params.card.id+ "'>"); frm.append("<input type='hidden' name='request_id' value='" + data + "'>"); frm.submit(); } } 作为字典本身。所以你应该这样称呼它:

output