Generate user information using the python implementation


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 models.py return the format directly “k1 v1 k2 v2”

Step 1:

Modify the models.py 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.email, self.idcard, self.adress, self.phonenumber
     )

Step 2:

Modify views.py to integrate the strings into an ordered dictionary

 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 a list generator when you get a list, just like that

>>>[str(i).split() for i in msg][0]
>>>['email', '[email protected]', '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 %}
   <br>
   <tr>
     <td> {{key}} </td>
     <td> :{{ value }} </td>
     <td> <a href="#" rel="external nofollow" value="change{{ key }}">  Modify the {{ key }}
 </a> </td>
   </tr>
  {% endfor %}
</table>

That’s it. Yeah, that’s ugly