This article describes how to use python to generate user information, for more information about how to use python to generate user information, see this article.
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.
def str(self): return "email {0} idcard {1} adress {2} phonenumber {3}".format( self.email, self.idcard, self.adress, self.phonenumber )
Step 2:
Modify views. py to integrate strings into ordered dictionaries.
from collections import OrderedDict as ordic @login_required def msg(request): msg = UserMsg.objects.filter(whoami=request.user) for item in msg: msglist = str(item).split(" ") msgkey = msglist[::2] msgvalue = msglist[1::2] msgs = ordic(zip(msgkey,msgvalue)) context = {'msg':msgs} return render(request, 'usermsg/msg.html', context)
You can also use the list generator to retrieve the list, just like this.
>>>[str(i).split() for i in msg][0]>>>['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.
{% For key, value in msg. items %}
| {Key }} |
: {Value }} |
Modify {key }} |
{% Endfor %}