python model+Form的示例

class UserType(models.Model): caption=models.CharField(max_length=32) class UserInfo(models.Model): username=models.CharField(max_length=32,verbose_name='用戶名') pwd=models.CharField(max_length=32,verbose_name='密碼') user_type=models.ForeignKey(to='UserType',to_field='id',on_delete=models.CASCADE,verbose_name='職位')html

def mf(request): if request.method =='GET': obj=userInfoModelForm() return render(request,'mf.html',{'obj':obj}) else: obj=userInfoModelForm(request.POST) obj.is_valid() return render(request, 'mf.html', {'obj': obj})code

<!DOCTYPE html>orm

<html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/xinwen/mf/"> {% csrf_token %} <table> {{ obj.as_table}}csrf

</table>
     <input type="submit" value="提交"/>

 </form>

</body> </html>htm

相關文章
相關標籤/搜索