我的测试网站有几个搜索页面,每个页面都包含自己的搜索表单。 但其中一些页面非常相同,它们在 views.py 中的显示功能非常相似。
例如:
@csrf_exempt
def rooms(request):
submited = False
search_option = 'None'
search_param = 'None'
data = []
query = ''
try:
cnx = mysql.connector.connect(user='root', password='****', host='127.0.0.1', database='lessons')
cursor = cnx.cursor()
if not submited:
query = "SELECT * FROM Rooms;"
cursor.execute(query)
data = cursor.fetchall()
if request.method == 'POST':
submited = True
if request.POST['search_option'] != "" and request.POST['search_param'] != "":
search_option = request.POST['search_option']
search_param = request.POST['search_param']
query = "SELECT * FROM Rooms WHERE " + search_option + " = " + search_param + ';'
cursor.execute(query)
data = cursor.fetchall()
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
return render(request, "rooms.html",
{"page_name": "Rooms", "search_option": search_option, "search_param": search_param,
"submited": submited, "dbdata": data, "query": query})
@csrf_exempt
def teachers(request):
submited = False
search_option = 'None'
search_param = 'None'
data = []
try:
cnx = mysql.connector.connect(user='root', password='****', host='127.0.0.1', database='lessons')
cursor = cnx.cursor()
if submited == False:
query = "SELECT * FROM Teachers;"
cursor.execute(query)
data = cursor.fetchall()
if request.method == 'POST':
submited = True
if 'search_option' in request.POST and 'search_param' in request.POST:
search_option = request.POST['search_option']
search_param = request.POST['search_param']
query = "SELECT * FROM Teachers WHERE " + search_option + " = '" + search_param + "';"
cursor.execute(query)
data = cursor.fetchall()
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
return render(request, "teachers.html",
{"page_name": "Teachers", "search_option": search_option, "search_param": search_param,
"submited": submited, "dbdata": data, "query": query})
我想知道如何制作几个类似的搜索网址(有一些非常小的更改,在我的情况下,它是一个不同的初始SQL查询来显示一般结果)引用一个函数(可能是由于某些if语句决定哪个页面显示)而不是几个完全相同,避免代码重复。
urls.py
urlpatterns = patterns('',
url(r'rooms', 'lessons.views.rooms', name='rooms'),
url(r'teachers', 'lessons.views.teachers', name='teachers'),
url(r'subjects', 'lessons.views.subjects', name='subjects'))
提前致谢。
答案 0 :(得分:2)
请求路由器配置(您可以调用urls.py
)完全是为了保持请求映射和视图代码分离。您也可以将其用于视图重用。
如果您查看一些django自己的class based views示例,您会发现:
(r'^about/', TemplateView.as_view(template_name="about.html"))
这或多或少正是您所建议的:对不同类型的URL重复使用相同的视图代码。您也可以为普通视图模拟它:
def my_view(request, param=None):
[...]
from functools import partial
url(r'^my_view0/', partial(views.my_view, param=0)),
url(r'^my_view1/', partial(views.my_view, param=1))
答案 1 :(得分:0)
您可以从每个网址使用相同的视图功能,例如lessons.views.generic_view
,您可以根据视图的名称进行区分,如下所示:
from django.core.urlresolvers import resolve
def generic_view(request):
view_name = resolve(request.path).view_name
if view_name == 'teachers':
...
您的样本urls.py
:
urlpatterns = patterns('',
url(r'rooms', 'lessons.views.generic_view', name='rooms'),
url(r'teachers', 'lessons.views.generic_view', name='teachers'),
url(r'subjects', 'lessons.views.generic_view', name='subjects')
)