为什么这个循环没有运行?

时间:2014-02-12 16:06:00

标签: python django model control-flow

如您所见,此代码允许用户创建名为Images的模型。问题是,当我想要它们时,实际上没有创建图像。在我多次输入信息后,带有令人讨厌的大写字母(打印图像)的打印测试返回一个空列表。

也许与此问题有关,我根本无法在代码中的任何if / else循环中添加打印测试。它会返回缩进错误,即使我检查了四个空格的所有缩进。

我真的很困惑。我怀疑我误解了控制流程?

views.py:

from django.shortcuts import render
from images_app.models import Image

def index(request):
    images = Image.objects.all()
    image_names = [a.name for a in images]
    print images        ###  THIS RETURNS AN EMPTY LIST!!

    if request.method == 'POST':

        image_string = request.POST.get('get_image')
        index = image_string.find('(')

        # divide input into parent and child
        if index == -1:
            parent = image_string
            child = None
        else:
            parent = image_string[0:index]
            child = image_string[index+1:len(image_string)-1]
         #  print "Child is.. ", child.name   ###  THIS RETURNS AN INDENTATION ERROR

        # create models based on input
        if parent not in image_names and child not in image_names:

            parent_model = Image(name=parent)
            child_model = Image(name=child, parent=parent_model)

        elif parent in image_names and child not in image_names:

            parent_model = images.get(name=parent)
            child_model = Image(name=child, parent=parent_model)

        elif parent not in image_names and child in image_names:

            child_model = images.get(name=child)
            parent_model = Image(name=parent)
            child_model.parent = parent_model
            print "descendants are: ", parent_model.get_descendants()

        else:
            print "else"

    return render(request, 'images_app/index.html', {'images':images})

def get_images(request):
    term = request.GET.get('terms') #jquery-ui.autocomplete parameter
    images = Image.objects.filter(name__istartswith=terms) 
    res = []
    for i in images:
         #make dict with the metadatas that jquery-ui.autocomple needs 
         dict = {'id':i.id, 'label':i.__unicode__(), 'value':i.__unicode__()}
         res.append(dict)
    return HttpResponse(simplejson.dumps(res))  

的index.html:

<!DOCTYPE html>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
        <title> images </title>
        <script>
        <script type="text/javascript">
        $(function() {
        $("#terms").autocomplete({
            source: "{% url 'images_app:get_images' %}",
            minLength: 1,
            });
        });
        </script>            
    </head>
    <nav>
    <a href="/associate">associate</a>
    </nav>
    <body>

        {% for images in images %}
            {{ image.name }} 
        {% endfor %}

        <section>
            <h1>create an image</h1>

            <form action="{% url 'images_app:index' %}" method="post">
                {% csrf_token %}

                <div class="ui-widget">
                    <label for="get_image">create image: </label>
                    <input id="get_image" name="get_image">
                </div>
                <div id="buttons">
                    <input type="submit" value="create" class="submit" name="create" />
                </div>
            </form>
        </section>

    </body>

1 个答案:

答案 0 :(得分:4)

您永远不会保存任何图片。

执行Image(blah)只是实例化内存中的对象。您需要在实例上调用.save(),或者Image.objects.create(blah)同时实例化并保存。