1.jsonp内容
from django.shortcuts import render # Create your views here. def upload(request): if request.method == "POST": file_obj = request.FILES.get("file") with open(file_obj.name, "wb") as f: for chunk in file_obj.chunks(): f.write(chunk) # 已经将文件保存在服务端,现在开始进行代码统计 code_line = 0 comment_line = 0 flag = False with open(file_obj.name, "r", encoding="utf8") as f2: for line in f2: if line.strip(): if flag and not line.strip().startswith('"""'): comment_line += 1 else: if line.strip().startswith("#"): comment_line += 1 elif line.strip().startswith('"""'): if not flag: flag = True else: flag = False comment_line += 1 else: code_line += 1 return render(request, "show.html", {"filename": file_obj.name, "code_line": code_line, "comment_line": comment_line}) return render(request, "upload.html")
2.modelForm
1. modelform :https://www.cnblogs.com/liwenzhou/p/8747872.html 1. 手敲代码实现书籍表的增改查 2. 用form组件实现增改查 1. 在页面展示html时 {% for filed in form_obj %} <p> {{ filed.label }} {{ filed }} </p> {% endfor %} 2. ChoiceField -> ModelChoiceField -> ModelMultipleChoiceField 1. ModelChoiceField 生成select标签 option选项是从queryset中获取的 2. ModelMultipleChoiceField 生成多选的select标签 option选项是从queryset中获取 3. form_obj如何设置初始化的值 from django.forms.models import model_to_dict --> Django 内置的把ORM对象转换成字典的工具函数 obj_dict = model_to_dict(book_obj) form_obj = forms.BookForm(initial=obj_dict) 3. 使用modelform来做书籍的增改查 class BookModelForm(forms.ModelForm): class Meta: # 告诉Django这个form类和那个model类对应 model = models.Book # 告诉Django这个form类里面有哪些字段 # fields = "__all__" # fields = ["title", "price"] exclude = ["authors"] widgets = { "publish_date": forms.widgets.DateInput( attrs={"type": "date", "class": "form-control"} ) } labels = { "title": "书名", "price": "价格" } error_messages = { "title": { "required": "书名不能为空" } } 2. Django logging模块的使用:https://www.cnblogs.com/liwenzhou/p/8763264.html