Skip to content

Commit

Permalink
表单提交我要学习咨询功能
Browse files Browse the repository at this point in the history
  • Loading branch information
zaxlct committed Apr 6, 2017
1 parent c8db114 commit 02c715f
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 30 deletions.
35 changes: 35 additions & 0 deletions apps/organization/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
__author__ = 'zaxlct'
__date__ = '2017/4/6 下午12:14'

import re

from django import forms
from operation.models import UserAsk


# class UserAskForm(forms.Form):
# name = forms.CharField(required=True, min_length=2, max_length=20)
# phone = forms.CharField(required=True, min_length=11, max_length=11)
# course_name = forms.CharField(required=True, min_length=5, max_length=50)


class UserAskForm(forms.ModelForm):
# 还可以新增字段
# price = forms.CharField(required=True, min_length=2, max_length=20)


class Meta:
model = UserAsk
fields = ['name', 'mobile', 'course_name']


# def clean_name(self):
# def clean_course_name(self):
def clean_mobile(self):
# 手机号验证
mobile = self.cleaned_data['mobile']
p = re.compile('^0\d{2,3}\d{7,8}$|^1[358]\d{9}$|^147\d{8}')
if p.match(mobile):
# 这里还能返回外键
return mobile
raise forms.ValidationError('手机号码格式不对', code='mobile_inval')
20 changes: 20 additions & 0 deletions apps/organization/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
__author__ = 'zaxlct'
__date__ = '2017/4/6 下午12:13'

from django.conf.urls import url

from .views import OrgView, AddUserAskView

urlpatterns = [
#课程机构列表页
url(r'^list/$', OrgView.as_view(), name='org_list'),
url(r'^add_ask/$', AddUserAskView.as_view(), name='add_ask'),

# url(r'^home/(?P<org_id>\d+)/$', OrgHomeView.as_view(), name='org_home'),
# url(r'^course/(?P<org_id>\d+)/$', OrgCourseView.as_view(), name='org_course'),
# url(r'^desc/(?P<org_id>\d+)/$', OrgDescView.as_view(), name='org_desc'),
# url(r'^teacher/(?P<org_id>\d+)/$', OrgTeacherView.as_view(), name='org_teacher'),
#
# # 课程机构收藏/取消收藏
# url(r'^add_fav/$', AddFavView.as_view(), name='add_fav'),
]
20 changes: 19 additions & 1 deletion apps/organization/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import json

from django.shortcuts import render
from django.views.generic import View
from django.http import HttpResponse
# Create your views here.

from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
from .models import CourseOrg, CityDict
from .forms import UserAskForm


# 课程机构
# 课程机构列表页,筛选页
class OrgView(View):
def get(self, request):
all_orgs = CourseOrg.objects.all()
Expand Down Expand Up @@ -52,3 +56,17 @@ def get(self, request):
'category': category,
'hot_orgs': hot_orgs,
})


# 用户添加咨询课程表单提交
class AddUserAskView(View):
def post(self, request):
user_ask_form = UserAskForm(request.POST)
res = dict()
if user_ask_form.is_valid():
user_ask_form.save(commit=True)
res['status'] = 'success'
else:
res['status'] = 'fail'
res['msg'] = '添加出错'
return HttpResponse(json.dumps(res), content_type='application/json')
6 changes: 2 additions & 4 deletions imooc/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

# from users.views import user_login
from users.views import LoginView, RegisterView, ActiveUserView, ForgetPwdView, ResetView, ModifyPwdView
from organization.views import OrgView

urlpatterns = [
url(r'^admin/', xadmin.site.urls),
Expand Down Expand Up @@ -59,9 +58,8 @@
url(r'^modify_pwd/$', ModifyPwdView.as_view(), name='modify_pwd'),


#课程机构首页
url('^org_list/$', OrgView.as_view(), name='org_list'),
# url(r'^org/$', include('organization.urls', namespace='org')),
#课程机构相关 URL
url(r'^org/', include('organization.urls', namespace='org')),
]


Expand Down
26 changes: 2 additions & 24 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
{% block custom_css %}{% endblock %}
<script src="{% static 'js/jquery.min.js' %}" type="text/javascript"></script>
<script src="{% static 'js/jquery-migrate-1.2.1.min.js' %}" type="text/javascript"></script>
{% block custom_js %}{% endblock %}

</head>
<body>
Expand Down Expand Up @@ -42,7 +41,7 @@
</span>
<ul class="selectMenu" id="jsSelectMenu">
<li data-value="course">公开课</li>
<li data-value="org">课程机构</li>
<a href="{% url 'org:org_list' %}"><li data-value="org">课程机构</li></a>
<li data-value="teacher">授课老师</li>
</ul>
</div>
Expand Down Expand Up @@ -132,27 +131,6 @@
<script src="{% static 'js/plugins/jquery.scrollLoading.js' %}" type="text/javascript"></script>
<script src="{% static 'js/deco-common.js' %}" type="text/javascript"></script>

<script>
$(function () {
$('#jsStayBtn').on('click', function () {
$.ajax({
cache: false,
type: "POST",
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>

{% block custom_js %}{% endblock %}
</body>
</html>
32 changes: 31 additions & 1 deletion templates/org-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ <h1>{{ org.name }}</h1>
<input type="text" name="course_name" id="companyAddress" placeholder="课程名" maxlength="50"/>
</div>
<p class="error company-tips" id="jsCompanyTips"></p>
<input class="btn" type="text" id="jsStayBtn" value="立即咨询 >"/>
<input class="btn" type="text" id="jsStayBtn" value="立即咨询"/>
{% csrf_token %}
</form>
</div>

Expand All @@ -141,3 +142,32 @@ <h1>{{ org.name }}</h1>
</section>

{% endblock %}
{% block custom_js %}
<script>
$(function () {
$('#jsStayBtn').on('click', function () {
$.ajax({
cache: false,
type: "POST",
dataType: "json",
url: "{% url 'org:add_ask' %}",
//表单提交可以用 serialize 方法把 csrf token 一块序列化过来
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)
}
},
error: function(error) {
console.log('error')
// console.log(error.responseText.msg)
}
});
});
})
</script>
{% endblock %}

0 comments on commit 02c715f

Please sign in to comment.