简体   繁体   English

Django:如何将在随机函数中生成的变量存储在数据库中?

[英]Django: How can I store in database a variable which I generate in a random function?

My web application is in django and it displays random images every time the user press the 'next' button. 我的Web应用程序在django中,每当用户按下“下一步”按钮时,它就会显示随机图像。 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. 例如,如果名称是“ image4.jpg”,那么我想将数字4存储为数字或文本。

So, first I have a class in the models.py file. 所以,首先我在models.py文件中有一个类。

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. 我在views.py文件中生成数字。 So, I just want to store it. 所以,我只想存储它。

As I said I have a function in the views.py file which generates the random numbers. 如我所说,我在views.py文件中有一个函数,该函数生成随机数。

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. 因此,这里我生成5个数字的列表,因为我想在每个会话中显示5张图像。 The user presses 5 times the 'next' button and each time he can see a new random image. 用户按下5次“下一步”按钮,每次他看到新的随机图像。

I have also in views.py five classes for the five Pages. 我还在views.py中有五个页面的五个类。

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. 第一次会话后的数据库必须有5列,每列中有一个数字。

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....

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. 另外,您应该考虑将Django会话用于您要执行的操作。 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: 并且在您的URL配置中,确保像这样编写它,以便通过图像编号正确地设定视图:

urlpatterns = [
    url(r'^images/([1-5])/$', views.image_view),
    # ...
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在数据库中存储和检索 Django 会话变量? - How can I store and retrieve Django session variable to/from database? 如何在Python中调用带有存储为字符串的变量的函数 - How in Python can I call a function with a variable which is store as a string 如何通过调用函数从此元素生成随机项目? - How can I generate a random item from this element by calling the function? django 中如何为变量添加默认值并存储在数据库中? - How do I add a default value to a variable and store in the database in django? Python 随机模块:如何生成包含特定数字的随机数? - Python random module: How can I generate a random number which includes certain digits? 如何在 python 3 中生成随机 function? - How do i generate random function in python 3? 如何在 django 用户 model 中生成随机唯一用户 ID 值并将其显示在 django 管理员上? - How can I generate a random unique User id value in django user model and show it on django admin? 如何使用 django 在 Mysql 数据库中存储文件路径? - How can I store a file path in Mysql database using django? 如何在Python中生成随机数? - How can I generate random numbers in Python? 我如何在pygame中随机生成立方体? - HOW i can generate cubes random in pygame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM