Generate a unified information display, modify the page, and generate a unified display page
During the exercise today, we want to display a page from the user information list to make information and modify it together, so that users can modify the content easily.
Considering that the information and values should be kept open, it must be a dictionary, because it is necessary to keep the location unchanged and use an ordered dictionary.
Considering the convenience and advantages of parsing, the format returned by models. py is "k1 v1 k2 v2 ".
Step 1:
Modify the models. py file, remove all the previous delimiters, and use spaces to separate them.
In order to save time and make a readable compromise, no keywords are used when formatted strings are not used.
1 def __str__(self):2 return "email {0} idcard {1} adress {2} phonenumber {3}".format(3 self.email, self.idcard, self.adress, self.phonenumber4 )
Step 2:
Modify views. py to integrate strings into ordered dictionaries.
1 from collections import OrderedDict as ordic 2 3 @login_required 4 def msg(request): 5 msg = UserMsg.objects.filter(whoami=request.user) 6 7 for item in msg: 8 msglist = str(item).split(" ") 9 10 msgkey = msglist[::2]11 msgvalue = msglist[1::2]12 msgs = ordic(zip(msgkey,msgvalue))13 context = {'msg':msgs}14 15 return render(request, 'usermsg/msg.html', context)
You can also use the list generator to retrieve the list, just like this.
1 >>>[str(i).split() for i in msg][0]2 >>>['email', 'xxxxxx@163.com', 'idcard', '12', 'adress', '13', 'phonenumber', '14']
The page is displayed. It is simply put into the table and no further processing is performed.
1 <table border = "0"> 2 {% for key, value in msg. items %} 3 <br> 4 <tr> 5 <td >{{ key }}</td> 6 <td>: {value }}</td> 7 <td> <a href = "#" value = "change {key}"> modify {key} 8 </ a> </td> 9 </tr> 10 {% endfor %} 11 </table>
That's it. Well, it's ugly.