1

My web application is in django and it displays random images every time the user press the 'next' button. I try to store the name or better the number of the image which is concatenated on its name. For example, if the name is 'image4.jpg' then I want to store the number 4 as number or as text.

So, first I have a class in the models.py file.

class Session(User):
    image_number = models.IntegerField()

The type of the variable is probably wrong because I don't want to get this from a form. I generate the number in the views.py file. So, I just want to store it.

As I said I have a function in the views.py file which generates the random numbers.

def func_random(self):
    random_im_list = random.sample(xrange(1,20),5)
    return {1v: random_im_list[0],
            2v: random_im_list[1],
            ... etc}

So, here I generate a list of 5 numbers, because I want to display 5 images in every session. The user presses 5 times the 'next' button and each time he can see a new random image.

I have also in views.py five classes for the five Pages.

class Image1(Page):

class Image2(Page):

class Image3(Page):

class Image4(Page):

class Image5(Page):

Here I need some help because I don't wait any input from the user. I already have generated the list with the random numbers. So, how can I store them on the database? The database after the first session must have 5 columns with one number in each column.

And after that I have the template file:

    {% if Page == 1 %}

        <div>
             <im src="{{static_url}}images/image{{ 1v }}.jpg" />   
        <div>
    {% elif Page == 2 %}
        <div>
             <im src="{{static_url}}images/image{{ 2v }}.jpg" />   
        <div>

etc....

1 Answer 1

1

It might make more sense for you to have a single view that takes the image number as an argument. You can then check to see if it's the first image, and if so, generate a list of random images for the user to see. Also, you should consider using Django sessions for what you're trying to do. If you use sessions, you could write your view like this:

from django.shortcuts import render

def image_view(request, image_number): # image_number is from 1 to 5
    if image_number == 1:
        request.session['image_list'] = list(random.sample(xrange(1,20),5))
    image = request.session['image_list'][image_number - 1]
    return render(request, "yourtemplate.html", {"image_number": image_number, "image": image})

Your template could look something like this:

<div>
    <img src="{{static_url}}images/image{{ image }}.jpg" />
</div>

And in your URL configuration, make sure to write it like this so that your view is properly parameterized by the image number:

urlpatterns = [
    url(r'^images/([1-5])/$', views.image_view),
    # ...
]
Sign up to request clarification or add additional context in comments.

3 Comments

Hi and thank you for your answer. If I don't want to use sessions, which is the type for the variable in the models.py file. I am noob in django and I don't understand it very well. When you create a class in the models.py file, the attributes of this class become columns on the database, right?
You could use a CommaSeparatedIntegerField. Then the image numbers would be stored like "5,7,1,14,11", so you'd have to put them into a string.
You could also use django-picklefield, which can store python objects directly into the database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.