我正在关注djangorest框架教程并构建一个博客应用程序。但是,当我尝试将数据呈现为HTML表单时,我遇到了“NoReverseMatch”错误。
以下是我的models.py
from django.db import models
class BlogModel(models.Model):
blog_title = models.CharField(max_length = 50)
blog_post = models.TextField(max_length = 100)
blog_author = models.CharField(max_length = 30)
blog_tags = models.CharField(max_length = 30)
def __str__(self):
return self.blog_title
class Meta:
ordering = ('blog_title',)
serializers.py
from rest_framework import serializers
from blogg.models import *
class BlogSerializer(serializers.ModelSerializer):
class Meta:
model = BlogModel
fields = ('blog_title', 'blog_post', 'blog_author', 'blog_tags')
views.py
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from django.http import Http404
from blogg.models import *
from blogg.serializers import *
from rest_framework.renderers import TemplateHTMLRenderer
class BlogList(APIView):
serializer_class = BlogSerializer
renderer_classes = [TemplateHTMLRenderer]
template_name = 'home.html'
def get(self, request, format = None):
blogs = BlogModel.objects.all()
#serializer = BlogSerializer(blogs, many = True)
#return Response(serializer.data)
return Response({'blogs':blogs})
def post(self, request, format = None):
serializer = BlogSerializer(data = request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors)
class BlogDetail(APIView):
serializer_class = BlogSerializer
renderer_classes = [TemplateHTMLRenderer]
template_name = "details.html"
def get_object(self, pk):
try:
return BlogModel.objects.get(pk = pk)
except BlogModel.DoesNotExist:
raise Http404
def get(self, request, pk, format = None):
blogs = self.get_object(pk)
serializer = BlogSerializer(blogs)
#return Response(serializer.data)
return Response({'serializer':serializer, 'blogs':blogs})
def put(self, request, pk, format = None):
blogs = self.get_object(pk)
serializer = BlogSerializer(blogs, data = request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors)
def delete(self, request, pk, format = None):
blogs = self.get_object(pk)
blogs.delete()
serializer = BlogSerializer(blogs)
return Response(serializer.data)
urls.py
from django.conf.urls import url, include
from blogg import views
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
url(r'^blogs/$', views.BlogList.as_view()),
url(r'^blogs/(?P<pk>[0-9]+)/$', views.BlogDetail.as_view()),
]
urlpatterns = format_suffix_patterns(urlpatterns)
details.html
{% load rest_framework %}
<html><body>
<h1>Blog - {{ blogs.title }}</h1>
<form action="{% url 'details' blogs.id %}" method="POST">
{% csrf_token %}
{% render_form serializer %}
<input type="submit" value="Save">
</form>
</body></html>
错误说明:
Reverse for 'details' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
行:<form action="{% url 'details' blogs.id %}" method="POST">
我想尝试访问的网址:
http://127.0.0.1:8000/blogg/blogs/1/
你可以帮我解决这个问题。如有任何其他信息,请告诉我
答案 0 :(得分:0)
您没有将name
作为关键字参数传递到您的网址函数中,以便使用已命名的网址。
这样的事应该可以正常工作:
url(r'^blogs/(?P<pk>[0-9]+)/$', views.BlogDetail.as_view(), name='details')
答案 1 :(得分:0)
最后,我解决了这个问题。我只是在我的网址上添加了一个名称,并在模板中使用了相同的名称,如下所示
在urls.py中添加了name属性
from django.conf.urls import url, include
from blogg import views
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
url(r'^blogs/$', views.BlogList.as_view()),
url(r'^blogs/(?P<pk>[0-9]+)/$', views.BlogDetail.as_view(), name = 'blog_details'),
]
urlpatterns = format_suffix_patterns(urlpatterns)
details.html
{% load rest_framework %}
<html><body>
<h1>Blog - {{ blogs.title }}</h1>
<form action="{% url 'blog_details' blogs.id %}" method="POST">
{% csrf_token %}
{% render_form serializer %}
<input type="submit" value="Save">
</form>
</body></html>
它的工作:)