html - Django Rest - Create method, returns error 405 Method Not Allowed -
i created view create new object. in drf default page works fine, when try create simple html page user interface (by adding renderer_classes , template_name view), following error occurs: 405 method not allowed.
i created html page retrieve/update client detail information, , works fine. however, unable create new client.
models.py
class client(models.model): client_rut = models.charfield(max_length=12, unique=true, verbose_name="rut") client_name = models.charfield(max_length=250, verbose_name="nombre cliente") client_region = models.charfield(max_length=50, verbose_name="region") client_tel = models.charfield(max_length=20, blank=true, verbose_name="telefono") client_email = models.emailfield(blank=true, verbose_name="e-mail") is_active = models.booleanfield(default=true, verbose_name="activo") class meta: ordering = ['client_rut'] def __str__(self): return self.client_rut + ' - ' + self.client_name
urls.py
urlpatterns = [ # /clients/ url(r'^$', views.clientview.as_view(), name='clients'), url(r'^(?p<pk>[0-9]+)/$', views.clientdetailview.as_view(), name='clientdetail'), url(r'^create/$', views.clientcreateview.as_view(), name='clientcreate') ] urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'html', 'xml'])
serializers.py
class clientlistserializer(modelserializer): url = hyperlinkedidentityfield(view_name='clients:clientdetail',) class meta: model = client fields = '__all__' class clientdetailserializer(modelserializer): class meta: model = client fields = '__all__'
views.py
#this view works fine class clientdetailview(views.apiview): queryset = client.objects.all() serializer_class = clientdetailserializer renderer_classes = [templatehtmlrenderer] template_name = 'clientdetail.html' def get(self, request, pk): client = get_object_or_404(client, pk=pk) serializer = clientdetailserializer(client) return response({'serializer': serializer, 'client': client}) def post(self, request, pk): client = get_object_or_404(client, pk=pk) serializer = clientlistserializer(client, data=request.data) if not serializer.is_valid(): return response({'serializer': serializer, 'client': client}) serializer.save() return redirect('clients:clients') # 1 not class clientcreateview(generics.listcreateapiview): queryset = client.objects.all() serializer_class = clientlistserializer renderer_classes = [templatehtmlrenderer] template_name = 'clientcreate.html' def post(self, request): client.objects.create() return redirect('clients:clients')
clientcreate.html
{% load rest_framework %} <html><body> <form action="{% url 'clients:clientcreate' %}" method="post"> {% csrf_token %} <div class="col-xs-6 "> {% render_form serializer %} </div> <div> <input type="submit" value="grabar"> </div> </form> </body></html>
clientdetail.html identical clientcreate.html, save this:
<form action="{% url 'clients:clientdetail' pk=client.pk %}" method="post">
i think i've seen 405 related questions , couldn't solve problem (doesn't mean answer's not out there though).
hope can me one.
thanks!
Comments
Post a Comment