Generate user information using the python implementation

  • 2020-05-27 06:02:34
  • OfStack

In today's exercise, I will show you a list of user information, and I want to make it into information and modify it in a webpage starting from 1, so that users can modify the content easily

Given the need to separate information and values, it must be a dictionary, because you need to keep the location the same and use an ordered dictionary

Considering the need for ease of parsing and good looking points, let return the format directly "k1 v1 k2 v2"

Step 1:

Modify the file to remove all the previous delimiters and use Spaces

For the sake of ease and readability, you don't use keywords when you don't use formatted strings

 def __str__(self):
     return "email {0} idcard {1} adress {2} phonenumber {3}".format(, self.idcard, self.adress, self.phonenumber

Step 2:

Modify to integrate the strings into an ordered dictionary

 from collections import OrderedDict as ordic
 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 a list generator when you get a list, just like that

>>>[str(i).split() for i in msg][0]
>>>['email', '', 'idcard', '12', 'adress', '13', 'phonenumber', '14']

Finally, it was displayed on the page and simply put into the table, without any further processing

 <table border="0">
   {% for key,value in msg.items %}
     <td> {{key}} </td>
     <td> :{{ value }} </td>
     <td> <a href="#" rel="external nofollow" value="change{{ key }}">  Modify the {{ key }}
 </a> </td>
  {% endfor %}

That's it. Yeah, that's ugly

Related articles: