1 编辑和添加后跳转页面:
思路:写一个参数将路径的条件带上
注意:捋流程的时候从urls里开始
1.在crm文件夹下新建python包:templatetags,在包里新建url.py
:
from django import template
from django.urls import reverse
register = template.Library()
@register.simple_tag()
def reverse_url(request, name, *args, **kwargs):
"""
反向解析生成URL,拼接参数
:param request:
:param name: URL的别名
:param args: pk等信息,可以按照位置参数传,也可以按照关键字参数传
:param kwargs:
:return:
"""
# 先反向解析生成URL,根据传的URL别名和参数反向解析生成基本的URL
base_url = reverse(name, args=args, kwargs=kwargs)
# 从当前的URL上获取参数 --> query=1&page=2
params = request.GET.urlencode # url变成了字符串
if not params:
return base_url
# 返回带条件的URL
return "{}?{}".format(base_url, params) # 有params的情况下再拼接URL
2.修改customer_list.html:
{% load url %} 引入url
修改操作
<td>
<a href="{% reverse_url request 'customer_edit' customer.pk %}"><i class="fa fa-pencil-square-o"></i></a>
</td>
3.在crm下的util文件里新建url.py
:
from django.urls import reverse
# 跳转页面,因为其他方法有可能也会用到,所以写成公共部分
def reverse_url(request, name, *args, **kwargs):
base_url = reverse(name, args=args, kwargs=kwargs)
params = request.GET.urlencode # url变成了字符串
if not params:
return base_url
return "{}?{}".format(base_url, params) # 有params的情况下再拼接URL
4.修改views.py
:
from crm.utils.url import reverse_url
# 新增和编辑
def customer_change(request, edit_id=None):
obj = models.Customer.objects.filter(pk=edit_id).first()
form_obj = CustomerForm(instance=obj)
if request.method == 'POST':
form_obj = CustomerForm(request.POST, instance=obj)
if form_obj.is_valid():
form_obj.save() # 没有instance新增 有instance做修改
# return redirect(reverse('customer_list'))
return redirect(reverse_url(request, 'customer_list'))
return render(request, 'customer_change.html', {'form_obj': form_obj, 'edit_id': edit_id})
2 跟进记录的展示、新增和编辑
展示跟进记录:
urls.py:
-> views.py:
class ConsultList(View):
def get(self, request):
return (request, 'consult_list.html')
->模板-consult_list.html:
->views.py:
# 获取跟进记录
all_consult = models.
->模板-consult_list.html:
->模板-layout.html:右侧添加跟进记录一栏
添加跟进记录:
urls.py:
-> views.py:
def consult_add(request):
pass
->forms.py:
class ConsultForm(BootstrapForm):
class Meta:
model = models.ConsultRecord
fields = "__all__"
-> views.py:
def consult_add(request):
# 实例化一个空Form
form_obj = ConsultForm()
return render(request,'consult_add.html', {'form_obj':form_obj})
->模板-consult_add.html:
->forms.py:
class ConsultForm(BootstrapForm):
class Meta:
model = models.ConsultRecord
fields = "__all__"
def __init__(*args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['delete_status'].widget.attrs.pop('class')
->views.py
def consult_add(request):
form_obj = ConsultForm()
# 处理
if request.method == 'POST':
form_obj = ConsultForm(request.POST)
return render(request,'consult_add.html', {'form_obj':form_obj})
->模板-consult_add.html:
编辑跟进记录
->urls.py:
url(r'^')
->views.py
def consult_add(request):
obj = models.ConsultRecord.objects.filter(pk=edit_id).first()
form_obj = ConsultForm()
# 处理
if request.method == 'POST':
form_obj = ConsultForm(request.POST)
return render(request,'consult_edit.html', {'form_obj':form_obj})
->模板-consult_edit.html:
->模板-consult_list.html:添加跳转链接
- 为每一名客户添加跟进记录编辑:
- 报名记录