Question

As you can see, this code lets the user create models called Images. The problem is, no images are actually created when I want them to be. The print test with the obnoxious caps (print images) returns an empty list after I've inputted information multiple times.

Perhaps related to this issue, I simply cannot add print tests to any of the if/else loops in the code. It returns an indentation error, even when I check all of the indents for four spaces.

I'm really confused. I suspect I'm misunderstanding the control flow?

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>
Was it helpful?

Solution

You don't ever save any of your images.

Doing Image(blah) simply instantiates an object in memory. You need to call .save() on the instance, or alternatively do Image.objects.create(blah) to instantiate and save at the same time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top