log处理问题:
添加机构信息的时候要上传机构的图片
在项目目录下面新建一个目录“media”,用来存放上传的图片
setting中要配置我们把文件存放在哪个根目录之下
#settings.py # 设置上传文件的路径 MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR,'media') #指定根目录
上面的media会将image = models.ImageField('logo',upload_to='org/%Y%m',max_length=100)的目录连接到一起。
这将会解决前端图片的正确取出。
因为,数据库存储的image路径是相对的:
具体前端实现:
data-url="{{ MEDIA_URL }}{{ course_org.image }}"
要向使用{{ MEDIA_URL }},要先在settings中TEMPLATES 里面添加media处理器:'django.core.context_processors.media'
然后也要添加处理图片相应路径的url
from django.views.static import serve from MxOnline.settings import MEDIA_ROOT # 处理图片显示的url,使用Django自带serve,传入参数告诉它去哪个路径找,我们有配置好的路径MEDIAROOT re_path(r'^media/(?P<path>.*)', serve, {"document_root": MEDIA_ROOT })
分页功能实现:
使用 分页神器 django-pure-pagination 分页,github上面有介绍使用方法
(1)安装
pip install django-pure-pagination
(2)settings里面添加
INSTALLED_APPS = ( ... 'pure_pagination', )
(3)view显示处理
# 这里指从allorg中取五个出来,每页显示5个 p = Paginator(all_orgs, 5, request=request) orgs = p.page(page)
ajax实现异步数据提交
场景:用户咨询
(1)在organazition目录下创建forms.py文件
# organization/forms.py from django import forms from operation.models import UserAsk class UserAskForm(forms.Form): '''我要咨询''' class Meta: model = UserAsk fields = ['name','mobile','course_name'] 复制代码
(2)include路由分发
删掉org_list的那个路由,改成include
path("org/", include('organization.urls', namespace="org")),
使用命名空间防止重复
然后在organization/urls.py中添加
# organization/urls.py from organization.views import OrgView from django.urls import path,re_path # 要写上app的名字 app_name = "organization" urlpatterns = [ path('list/',OrgView.as_view(),name='org_list'), ]
(3)视图函数
from django.http import HttpResponse from .forms import UserAskForm class AddUserAskView(View): """ 用户添加咨询 """ def post(self, request): userask_form = UserAskForm(request.POST) if userask_form.is_valid(): user_ask = userask_form.save(commit=True) # 如果保存成功,返回json字符串,后面content type是告诉浏览器返回的数据类型 return HttpResponse('{"status":"success"}', content_type='application/json') else: # 如果保存失败,返回json字符串,并将form的报错信息通过msg传递到前端 return HttpResponse('{"status":"fail", "msg":"添加出错"}', content_type='application/json')
(4)配置url
# organization/urls.py from organization.views import OrgView,AddUserAskView from django.urls import path,re_path # 要写上app的名字 app_name = "organization" urlpatterns = [ path('list/',OrgView.as_view(),name='org_list'), path('add_ask/', AddUserAskView.as_view(), name="add_ask"), ]
(5)在ModelForm中自定义一个手机号验证的方法
# organization/forms.py import re from django import forms from operation.models import UserAsk class UserAskForm(forms.ModelForm): class Meta: model = UserAsk fields = ['name', 'mobile', 'course_name'] def clean_mobile(self): """ 验证手机号码是否合法 """ mobile = self.cleaned_data['mobile'] REGEX_MOBILE = "^1[358]\d{9}$|^147\d{8}$|176\d{8}$" p = re.compile(REGEX_MOBILE) if p.match(mobile): return mobile else: raise forms.ValidationError(u"手机号码非法", code="mobile_invalid")
<script> $(function () { $('#jsStayBtn').on('click', function () { $.ajax({ cache: false, type: "POST", url: "{% url "org:add_ask" %}", data: $('#jsStayForm').serialize(), async: true, success: function (data) { if (data.status == 'success') { $('#jsStayForm')[0].reset(); alert("提交成功") } else if (data.status == 'fail') { $('#jsCompanyTips').html(data.msg) } }, }); }); }) </script>
# organization/forms.py from django import forms from operation.models import UserAsk class UserAskForm(forms.Form): '''我要咨询''' class Meta: model = UserAsk fields = ['name','mobile','course_name']
本文来自博客园,作者:topass123,转载请注明原文链接:https://www.cnblogs.com/topass123/p/12944809.html