Home >Backend Development >Python Tutorial >How to generate user information through python

How to generate user information through python

Y2J
Y2JOriginal
2017-05-04 15:30:311672browse

This article mainly introduces the relevant code for generating user information using python. It is very simple and practical. Friends who need it can refer to it

Today when practicing, I want to show a list of user information. Make it into a web page that combines information and modifications to facilitate users to modify the content

Considering that information and values ​​must be separated, it must be a dictionary, because the position needs to be guaranteed to remain unchanged, so use an ordered dictionary

Considering the need for convenient parsing and good looks, the format returned in models.py is directly the "k1 v1 k2 v2" format

Step one:

Modify models.py file, remove all the previous separators and use spaces to separate

In order to save trouble and make it readable, no keywords are used when formatting string is 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 and 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 the listgenerator when obtaining the list , just like this

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

Just display it on the page at the end, simply put it in the form, and no 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 }}"> 修改{{ key }}
 </a> </td>
   </tr>
  {% endfor %}
</table>

[Related recommendations]

1. Python Free Video Tutorial

2. Python Learning Manual

3. Python Object-Oriented Video Tutorial

The above is the detailed content of How to generate user information through python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn