'getting multi values from CSV to DB

I have a CSV file that goes like this

1,CA5D23 ,1410,111, Detail,"/assets/images/thumb/1537.png, /assets/images/thumb/1536.png"
2,C7F91E ,1525,109, Detail,/assets/images/thumb/1537.png, /assets/images/thumb/1452.png"
3,C4263D,1375,40, Detail,/assets/images/thumb/1537.png, /assets/images/thumb/1201.png"
4,C1595W,600,39, Detail,/assets/images/thumb/1537.png, /assets/images/thumb/1136.png"
5,C3145M,585,39, Detail,/assets/images/thumb/1537.png, /assets/images/thumb/0216.png"


the URLs for images are in the same cell. This is my model.py

class Account(models.Model):
    code = models.IntegerField(__("code"))
    orbs = models.IntegerField(__("orbs"))
    price = models.IntegerField(__("price"))
    detail = models.CharField(__("detail"), max_length=255)
    image = models.ImageField(__("image"))  

   


I then import the CSV file to the DB using sqlite3.exe db.sqlite3 command. After that, I go to my index.html and write this code.

<tr >
                {% for account in account  %}
                            <td style="text-align:center;vertical-align:middle;">{{account.code}}</td>
                            <td style="text-align:center;vertical-align:middle;"><img src=" {% static 'assets/images/assets/orb.png' %} " width="22" height="22" style="padding-bottom:3px;" />{{account.orbs}}</td>
                            <td style="text-align:center;vertical-align:middle;">{{account.price}}</td>
                            <td style="text-align:center;vertical-align:middle;"><a target="_blank" href=" {% static 'account61f3.html?id_xml=001660' %} " ><input type="button" class="btn btn-success btn-sm" value={{account.detail}} ></a></td>
                            </tr>
                            
                            <tr>
                                <td colspan="5">   
                                            
                                    <div class="parent"><img class="img" title="" src=" {% static account.image.url %} "width="64" height="64"></div>
 
                                </td>
                                
                            </tr> {% endfor %}<tr >

Focus on the account.image.URL which shows only one image on the site or none if there's more than one URL. How can I include more than one URL to the DB and iterate over it without messing with the next cell?

PS: I did try to change from ImageField to CharField and paste the whole image reference on the cell but it turned the < > sign to &lt and &gt: which made the reference into a string and not a HTML



Solution 1:[1]

A dictionary contains key-value pairs. Key has to be unique. So in this case dictionary is not a good way to go.

You will have to construct a domain model first:

//This will hold your user information
public class UserInfo
{
    public string Name { get; set; }
    public string Email { get; set; }
    public string Location { get; set; }
    public int Age { get; set; }
    public string Phone { get; set; }
}

//this other class is called a viewmodel which combains your user and the question posed by the user
public class FormData
{
    public UserInfo user { get; set; }
    public string question { get; set; }
}

//this class is your public facing interface which will interact with the user/front-end form

public class Questionaire
{
    FormData _data; 
    public void FillForm(FormData data)
    {
        //do whatever you want with the data
        //save to database, send by email, whatever
        _data = data;
        }
    }


    class Program
    {
        static void Main(string[] args)
        {

//this is how you use it to collect data from the user
            Questionaire questionaire = new Questionaire();
            questionaire.FillForm(new FormData
            {
                question = "your question goes here",
                user = new UserInfo
                {
                    Name = "YourName",
                    Age = 20,
                    Email = "someemail",
                    Location = "yourlocation",
                    Phone = "0123665555"
                }
            });
        }
    }

Option 2 (since you insist in using dictionary):

    public class UserInfo
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string Location { get; set; }
        public int Age { get; set; }
        public string Phone { get; set; }
    }


        Dictionary<string, UserInfo> _values;
        public void FillForm(Dictionary<string, UserInfo> values)
        {
            _values = values;
        }


//You call it like this:
            Dictionary<string, UserInfo> dic = new Dictionary<string, UserInfo>();
            dic.Add("question1", new UserInfo { Age = 20, Name = "Name1" });
            dic.Add("question2", new UserInfo { Age = 20, Name = "Name2" });

            Form form = new Form();
            form.FillForm(dic);

Option 3 but this is not the way to go:

public class Form2
{
    Dictionary<string, string> _values;
    public void FillForm(Dictionary<string, string> values)
    {
        _values = values;
    }
}


Dictionary<string, string> user1 = new Dictionary<string, string>();
user1.Add("question1", "Users question goes here");
user1.Add("Name", "name1");
user1.Add("Age", "age");
            //...
var form2 = new Form2();
form2.FillForm(user1);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1