Django:如何修复模板渲染?

时间:2017-04-19 08:51:44

标签: django

这是我的views.py

views.py:

from django.views import generic
from django.shortcuts import render, get_object_or_404
from .models import medicines,details_post,Post
from django.http import JsonResponse


def index(request):
    detailsposts=details_post.objects.all()
    medicine=medicines.objects.all()
    blog=Post.objects.all()
    return render(request,'aptool/index.html',{'my_dis':detailsposts,'my_data':medicine,'blogposts':blog})

def search(request):
    searchdata=request.GET.get('searchtext')
    queryset=details_post.objects.filter(disease_name__startswith=searchdata).values_list('disease_name')
    return JsonResponse({'result':list(queryset)})

class MedicineView(generic.ListView):
    template_name='aptool/medicine.html'

    def get_queryset(self):
        return medicines.objects.all()

class DiseaseView(generic.ListView):
    template_name='aptool/disease.html'

    def get_queryset(self):
        return details_post.objects.all()

class BlogView(generic.ListView):
    template_name='aptool/blog.html'

    def get_queryset(self):
        return Post.objects.all()



def meddetail(request, med_id):
    medicine=get_object_or_404(medicines,pk=med_id)
    return render(request,'aptool/meddetail.html',{'medicine':medicine})

def disdetail(request, dis_id):
    detailsposts=get_object_or_404(details_post,pk=dis_id)
    return render(request,'aptool/details_post.html',{'detailsposts':detailsposts})



def searchdetail(request):
    searchtext=request.POST['searchtext']
    try:
        detailsposts=details_post.objects.get(disease_name__startswith=searchtext)
    except:
        return render(request,'aptool/details_post.html',{'error_message':True})
    else:
        return render(request,'aptool/details_post.html',{'detailsposts':detailsposts})


def blogdetail(request,blog_id):
    blog= get_object_or_404(Post,pk=blog_id)
    return render(request,'aptool/blog_post.html',{'blog':blog})

虽然urls.py看起来像这样:

***urls.py***

from django.conf.urls import url, include
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^medicine/$',views.MedicineView.as_view(),name='medicine'),
    url(r'^disease/$',views.DiseaseView.as_view(),name='disease'),
    url(r'^blog/$',views.BlogView.as_view(),name='blogview'),
    url(r'^disease/(?P<dis_id>[0-9]+)/$',views.disdetail, name='disdetail'),
    url(r'^medicine/(?P<med_id>[0-9]+)/$', views.meddetail, name='meddetail'),
    url(r'blog/(?P<blog_id>[0-9]+)/$', views.blogdetail, name='blogdetail'),
    url(r'^disease/detail$',views.searchdetail,name='searchdetail'),
    url(r'^search/$',views.search,name='search'),
  ]

我尝试加载index.html时出现的问题是:

NoReverseMatch at /index/
Reverse for 'blog' with arguments '()' and keyword arguments '{}' not 
found. 0 pattern(s) tried: []

我无法弄清楚代码有什么问题。任何帮助都会很棒。

1 个答案:

答案 0 :(得分:3)

这种情况正在发生,因为您没有任何名为blog的urlconf。 也许您想使用blogview

blog替换为模板blogview模板标签中的url