def show_category(request, category_name_slug): # 创建上下文字典,稍后传给模板渲染引擎 context_dict = {} try: # 能通过传入的分类别名找到对应的分类吗? # 如果找不到,.get() 方法抛出 DoesNotExist 异常 # 因此 .get() 方法返回一个模型实例或者抛出异常 category = Category.objects.get(slug=category_name_slug) # 检索关联的所有网页 # 注意, filter()返回一个网页对象列表或者空列表 pages = Page.objects.filter(category=category) # 把得到的列表赋值给模板上下文中名为 pages 的键 context_dict['pages'] = pages context_dict['category'] = category except Category.DoesNotExist: context_dict['category'] = None context_dict['pages'] = None result_list = [] if request.method == 'POST': query = request.POST['query'].strip() if query: result_list = run_query(query) context_dict['query'] = query context_dict['result_list'] = result_list # 渲染响应,返回给客户端 return render(request, 'rango/category.html', context_dict)
def show_category(request, category_name_slug): context_dict = {} try: # Can we find a category name slug with the given name? # If we can't, the .get() method raises a DoesNotExist exception. # So the .get() method returns one model instance or raises an exception. category = Category.objects.get(slug=category_name_slug) # Retrieve all of the associated pages. # Note that filter() will return a list of page objects or an empty list pages = Page.objects.filter(category=category).order_by('-views') context_dict['pages'] = pages context_dict['category'] = category except Category.DoesNotExist: context_dict['category'] = None context_dict['pages'] = None context_dict['query'] = category.name result_list = [] if request.method == 'POST': query = request.POST['query'].strip() if query: result_list = run_query(query) context_dict['query'] = query context_dict['result_list'] = result_list return render(request, 'rango/category.html', context_dict)
def show_category(request, category_name_slug): context_dict = {} try: category = Category.objects.get(slug=category_name_slug) pages = Page.objects.filter(category=category).order_by('-views') context_dict['pages'] = pages context_dict['category'] = category except Category.DoesNotExist: context_dict['category'] = None context_dict['pages'] = None context_dict['query'] = category.name result_list = [] if request.method == 'POST': query = request.POST['query'].strip() if query: # Run our Webhose function to get the results list! result_list = run_query(query) context_dict['query'] = query context_dict['result_list'] = result_list return render(request, 'rango/category.html', context_dict)
def search(request): result_list = [] if request.method == 'POST': query = request.POST['query'].strip() if query: result_list = run_query(query) return render(request, 'rango/search.html', {'result_list': result_list})
def show_category(request, category_name_slug): context_dict = {} # Prepare category and page try: category = Category.objects.get(slug=category_name_slug) pages = Page.objects.filter(category=category) context_dict['pages'] = pages context_dict['category'] = category except Category.DoesNotExist: context_dict['pages'] = None context_dict['category'] = None # Prepare result list context_dict['queryString'] = category.name result_list = [] if request.method == 'POST': query = request.POST['query'].strip() if query: result_list = run_query(query) context_dict['queryString'] = query context_dict['result_list'] = result_list return render(request, 'rango/category.html', context_dict)
def search(request): result_list = [] if request.method == 'POST': query = request.POST['query'].strip() if query: # Run our Webhose function to get the results list! result_list = run_query(query) return render(request, 'rango/search.html', {'result_list': result_list})
def search(request): result_list = [] context_dict = [] if request.method == 'POST': query = request.POST['query'].strip() if query: result_list = run_query(query) context_dict = {'result_list': result_list, 'query': query} return render(request, 'rango/category.html', context_dict)
def search(request): result_list = [] context_dict = {} if request.method == 'POST': query = request.POST['query'].strip() if query: result_list = run_query(query) context_dict['query'] = query context_dict['result_list'] = result_list return render(request, 'rango/search.html', context=context_dict)
def search(request): result_list = [] query = '' if request.method == 'POST': query = request.POST['query'].strip() if query: result_list = run_query(query) context_dictionary = {'previous_query': query, 'result_list': result_list} return render(request, 'rango/search.html', context_dictionary)
def search(request): context_dict = {} result_list = [] if request.method == 'POST': query = request.POST['query'].strip() if query: # Run our Webhose search function to get the results list! result_list = run_query(query) context_dict['query'] = query context_dict['result_list'] = result_list return render(request, 'rango/search.html', context_dict)
def search(request): query = '' result_list = [] if request.method == 'POST': query = request.POST['query'].strip() if query: print(query) print(type(query)) # Run our Webhose search function to get the results list! result_list = run_query(query) context_dict = {'result_list': result_list, 'query': query} return render(request, 'rango/search.html', context_dict)
def search(request): result_list = [] request_text = '' if request.method == 'POST': query = request.POST['query'].strip() if query: # Запустите нашу функцию поиска Webhose, чтобы получить список результатов! result_list = run_query(query) request_text = query # если был запрос, возвращаю его в поле запроса return render(request, 'rango/search.html', { 'result_list': result_list, 'request_text': request_text })
def search(request): result_list = [] query = "" if request.method == 'POST': query = request.POST['query'].strip() if query: # Run Webhouse search function to get the list. result_list = run_query(query) return render(request, 'rango/search.html', { 'result_list': result_list, 'query': query })
def music(request): piece_list_date = Piece.objects.order_by('-title')[:5] piece_list_rating = Piece.objects.order_by('artist')[:5] context_dict = { 'piece_dates': piece_list_date, 'piece_rates': piece_list_rating } result_list = [] if request.method == "POST": query = request.POST['query'].strip() if query: result_list = run_query(query) context_dict['query'] = query context_dict['result_list'] = result_list return render(request, 'rango/music.html', context_dict)
def show_category(request, category_name_slug): # Create a context dictionary which we can pass # to the template rendering engine. context_dict = {} try: # Can we find a category name slug with the given name? # If we can't, the .get() method raises a DoesNotExist exception. # So the .get() method returns one model instance or raises an exception. category = Category.objects.get(slug=category_name_slug) # Retrieve all of the associated pages. # Note that filter() will return a list of page objects or an empty list pages = Page.objects.filter(category=category).order_by('-views') # Adds our results list to the template context under name pages. context_dict['pages'] = pages # We also add the category object from # the database to the context dictionary. # We'll use this in the template to verify that the category exists. context_dict['category'] = category except Category.DoesNotExist: # We get here if we didn't find the specified category. # Don't do anything - # the template will display the "no category" message for us. context_dict['category'] = None context_dict['pages'] = None # New code added here to handle a POST request # create a default query based on the category name # to be shown in the search box context_dict['query'] = category.name result_list = [] if request.method == 'POST': query = request.POST['query'].strip() if query: # Run our Webhose function to get the results list! result_list = run_query(query) context_dict['query'] = query context_dict['result_list'] = result_list # Go render the response and return it to the client. return render(request, 'rango/category.html', context_dict)
def show_category(request, category_name_slug): # Create a context dictionary which we can pass # to the template rendering engine. context_dict = {} try: # Can we find a category name slug with the given name? # If we can't, the .get() method raises a DoesNotExist exception. # So the .get() method returns one model instance or raises an exception. category = Category.objects.get(slug=category_name_slug) # Retrieve all of the associated pages. # Note that filter() returns a list of page objects or an empty list pages = Page.objects.filter(category=category) # Adds our results list to the template context under name pages. context_dict['pages'] = pages # We also add the category object from # the database to the context dictionary. # We'll use this in the template to verify that the category exists. context_dict['category'] = category # We get here if we didn't find the specified category. # Don't do anything - # the template will display the "no category" message for us. except Category.DoesNotExist: context_dict['category'] = None context_dict['pages'] = None # create a default query based on the category name # to be shown in the search box context_dict['query'] = category.name result_list = [] if request.method == 'POST': query = request.POST['query'].strip() if query: # Run our Webhose function to get the results list! result_list = run_query(query) context_dict['query'] = query context_dict['result_list'] = result_list # Go render the response and return it to the client. return render(request, 'rango/category.html', context_dict)
def show_page(request, page_name_slug): context_dict = {} try: page = Page.objects.get(slug=page_name_slug) context_dict['page'] = page except Page.DoesNotExist: context_dict['page'] = None context_dict['query'] = page.title result_list = [] if request.method == 'POST': query = request.POST['query'].strip() if query: # Run our Webhose function to get the results list! result_list = run_query(query) context_dict['query'] = query context_dict['result_list'] = result_list # Go render the response and return it to the client. return render(request, 'rango/page.html', context_dict)
def show_category(request, category_name_slug): # Create a context dictionary which we can pass to the template rendering engine. context_dict = {} try: # Can we find a category name slug with the given name? # If we can't, the .get() method raises a DoesNotExist exception # So the .get() method returns one model instance or raises an exception category = Category.objects.get(slug=category_name_slug) # Retrieve all of the associated pages # Note that filter() will return a list of pages or an empty list pages = Page.objects.filter(category=category).order_by('-views') # Adds our results list to the template context under name pages context_dict["pages"] = pages # We also add the category object from the database to the context dictionary # We'll use this in the template to verify that the category exists context_dict['category'] = category except Category.DoesNotExist: # We get here if we didn't find the specified category, don't do anything # the template will display the "no category" message for us context_dict['category'] = None context_dict['pages'] = None # If category doesn't exist, return early return render(request, 'rango/category.html', context_dict) # Set query to be the default category name context_dict['query'] = category.name result_list = [] # Use Webhose to get results (from online) if request.method == 'POST': query = request.POST['query'].strip() if query: result_list = run_query(query) context_dict['query'] = query context_dict['result_list'] = result_list # Go render the response and return it to the client return render(request, 'rango/category.html', context_dict)
def search(request): results_list = [] if request.method == 'POST': query = request.POST['query'].strip() cat_id = request.POST['category'] category = Category.objects.get(id=int(cat_id)) print('query:' + query) print('cat_id:' + cat_id) if query: results_list = run_query(query) context_dict = { 'results_list': results_list, 'query': query, 'category': category } return render(request, 'rango/category.html', context_dict)
def show_category(request, category_name_slug): # Create a context dictionary which we can pass # to the template rendering engine. context_dict = {} try: # .get() returns a model instance matching slug, or raises exception # if slug doesn't exist. category = Category.objects.get(slug=category_name_slug) # Retrieve list of page objects, or an empty list, and sort by views. pages = Page.objects.filter(category=category).order_by('-views') # Add results list to template context under name pages. context_dict['pages'] = pages # Add category object from database to context dictionary # to be used in template to verify that category exists. context_dict['category'] = category except Category.DoesNotExist: # Template will display "no category" message. context_dict['category'] = None context_dict['pages'] = None # Added for search form context_dict['query'] = category.name result_list = [] if request.method == 'POST': query = request.POST['query'].strip() if query: # Run our search API function to get the results list! result_list = run_query(query) context_dict['query'] = query context_dict['result_list'] = result_list # Go render the response and return it to the client. return render(request, 'rango/category.html', context_dict)