python - Should every url from urls.py in django have it's own method to respond the request? -
my test site has several search pages , each page contains it's own search form. of these pages identical , displaying functions in views.py similar.
for example:
@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 * rooms;" cursor.execute(query) data = cursor.fetchall() if request.method == 'post': submited = true if request.post['search_option'] != "" , request.post['search_param'] != "": search_option = request.post['search_option'] search_param = request.post['search_param'] query = "select * rooms " + search_option + " = " + search_param + ';' cursor.execute(query) data = cursor.fetchall() except mysql.connector.error err: if err.errno == errorcode.er_access_denied_error: print("something wrong user name or password") elif err.errno == errorcode.er_bad_db_error: print("database 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 * teachers;" cursor.execute(query) data = cursor.fetchall() if request.method == 'post': submited = true if 'search_option' in request.post , 'search_param' in request.post: search_option = request.post['search_option'] search_param = request.post['search_param'] query = "select * teachers " + search_option + " = '" + search_param + "';" cursor.execute(query) data = cursor.fetchall() except mysql.connector.error err: if err.errno == errorcode.er_access_denied_error: print("something wrong user name or password") elif err.errno == errorcode.er_bad_db_error: print("database 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})
i wondering how make several similar search urls (with small changes, in case it's different initial sql-query display general results) refer 1 function (maybe due if statement decide page display) rather several quite identical , avoid code repetition.
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'))
thanks in advance.
you can use same view function, let's lessons.views.generic_view
, each url , can discriminate based on view's name this:
from django.core.urlresolvers import resolve def generic_view(request): view_name = resolve(request.path).view_name if view_name == 'teachers': ...
your sample 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') )
Comments
Post a Comment