如何使用Django将所有搜索结果链接到特定链接

时间:2019-04-04 12:42:25

标签: python django

我的应用程序中有两个类模型,当我进行搜索时,我在搜索结果中得到了我的应用程序中的两个类模型的内容。我只能添加一个直接链接到搜索的类Model。那么,如何为我所有的课堂模型搜索结果添加单个链接?

我的public class CustomServiceLocator : IServiceLocator { private IServiceProvider _provider; public RatioDissectionServiceLocator(IServiceProvider provider) { _provider = provider; } public IEnumerable<object> GetAllInstances(Type serviceType) { throw new NotImplementedException(); } public IEnumerable<TService> GetAllInstances<TService>() { throw new NotImplementedException(); } public object GetInstance(Type serviceType) { throw new NotImplementedException(); } public object GetInstance(Type serviceType, string key) { throw new NotImplementedException(); } public TService GetInstance<TService>() { return _provider.GetService<TService>(); } }

Views.py

模板def news_story(request, slug, *args, **kwargs): print(args) print(kwargs) post = get_object_or_404(Post, slug=slug) form = CommentForm(request.POST, request.FILES) if request.method == "POST": if form.is_valid(): #form.instance.user = request.user form.instance.post = post form.save() form = CommentForm() return HttpResponseRedirect(reverse("news_story", kwargs = { 'slug': slug })) comment = Comment.objects.all().order_by("-timestamp") if comment: comment = comment.filter(post=post) try: itm = Post.objects.get(slug=slug) except Post.DoesNotExist: itm = None old_post = Post.objects.all()[:8] new_post = Post.objects.all().order_by("-timestamp")[:3] template = loader.get_template('news_story.html') context = { 'Post': itm, 'post': comment, 'form': form, 'old_post': old_post, 'new_post': new_post, } return HttpResponse(template.render(context, request)) def entertainment_story(request, slug, *args, **kwargs): print(args) print(kwargs) post = get_object_or_404(Entertainment, slug=slug) try: itm = Entertainment.objects.get(slug=slug) except Entertainment.DoesNotExist: itm = None template = loader.get_template('entertainment_story.html') context = { 'Entertainment': itm } return HttpResponse(template.render(context, request)) def search_results(request): query = request.GET.get('q') if query: post = Post.objects.filter( Q(title__icontains=query) | Q(author__icontains=query) | Q(description__icontains=query) ).distinct() entertainment = Entertainment.objects.filter( Q(title__icontains=query) | Q(author__icontains=query) | Q(description__icontains=query) ).distinct() queryset = chain(post, entertainment) context = { 'queryset': queryset, } return render(request, 'search_results.html', context) My Template Search_results <h2 >This is the result of your search:</h2> {% for post in queryset %} <div class="col-md-4"> <!-- COLS STARTS HERE --> <div class="thumbnail"> <a href="{% url 'news_story' slug=post.slug %}"> <img src="{{ MEDIA_URL }}{{ post.blog_image.url }}"> <div class="description"> <p>{{ post.description }}</p> </div> <div class="caption"> <p>{{ post.title }}</p> </div> </a> <div class="author"> <p>Posted on {{ post.timestamp }} <br />by {{ post.author }}</p> </div> </div> </div> <!-- COLS ENDS HERE --> {% empty %} <div class="row"> <!-- ROW STARTS HERE --> <h3 style="text-align: center">There is no search result</h3> <p style="text-align: center">If you are not happy with the result, please do another search.</p> </div> {% endfor %} </div> 中的href="news_story"是问题,它仅适用于search_results帖子,不适用于entertainment_story帖子。如何为news_storynews_story创建相同的链接。谢谢!

0 个答案:

没有答案