Django REST framework not looking for URL encoded detail views

django rest framework url parameters
django rest framework tutorial
django rest framework json response
django rest framework redirect
django rest framework response
django rest framework forms
django rest framework api root
django rest framework browsable api

I have a model whose Primary Key is a TextField. This is a minimalistic reproduction of my issue:


class Filename(models.Model):
    path = models.TextField(primary_key=True)


class FilenameSerializer(ModelSerializer):
    class Meta:
        model = Filename
        fields = '__all__'


class FilenameViewSet(ModelViewSet):
    queryset = Filename.objects.all()
    serializer_class = FilenameSerializer

I'm using a DefaultRouter for URLs. Here is the problem: If I sent {"path":"test"} with a POST /filename/ I can perfectly retrieve my object with GET /filename/test/ as you would expect. However, if I POST /filename/ something like {"path":"c:\\test"} I would expect either GET /filename/c%3A%5Ctest/ or GET /filename/c%3A%5C%5Ctest/ to be the proper way to get it, but none of those works. Does anybody knows what's going on?

Update: The webserver logs show Not Found: /filename/c:\test so it's being decoded properly at some moment. Maybe some URL regex issue?

The issue was not with any URL encoded in general, but with the dot (%2E) in particular. The DefaultRouter() does not match the dots by default. This behavior can be modified setting the lookup_value_regex attribute in the ViewSet.

class FilenameViewSet(ModelViewSet):
    queryset = Filename.objects.all()
    serializer_class = FilenameSerializer
    lookup_value_regex = '[^/]+'

It leaves less work for your API clients. There's no ambiguity about the meaning of the string when it's found in representations such as JSON that do not have a  Upon reading, it looks like the REST Framework is not looking up Phones by number, but processing the srcPhone attribute as if it was a URL. This would normally be true, but I want it to look up Phones by natural key, rather than provide the URL.

hey you have to define the lookupfield , you can overide the get_object change string format to a norma text

class FilenameViewSet(ModelViewSet):
   queryset = Filename.objects.all()
   serializer_class = FilenameSerializer
   lookup_field = 'path'

def get_object(self):
      from urllib import unquote
   except ImportError:
      from urllib.parse import unquote
  path = unquote(kwarhs['path'))
  return self.get_queryset.get(path=path)

Search Django REST framework The set of valid parsers for a view is always defined as a list of classes. using 'application/x-www-form-urlencoded' , which may not be what you wanted. See the Django documentation for more details. Django will not necessarily set a CSRF token in the header, unless it is rendering a template that explicitly has the csrf_token template tag included. This means that you need to request a page that renders a form with a csrf token, or you need to create a token-requesting view that is decorated with ensure_csrf_cookie.

if im getting your question correctly . you problem is with json format . On adding escape character \ before special characters would resolve your issue.

see this link below: How to escape special characters in building a JSON string?

Django, API, REST, Settings. A view inspector class that will be used for schema generation. Default: the search term used by SearchFilter . Default: search  The html format for the API is just a nicety of Django REST Framework, not really necesary, you usually would consume your API using json format. In any case, are you running this on your server or locally? Did you run the collectstatic managment command? – asermax Aug 19 '14 at 4:25

This allows you to return API schema or other resource information. There are not currently any widely adopted conventions for exactly what style of response  Django community: Django Q&A RSS This page, updated regularly, aggregates Django Q&A from the Django community. Django variables are URL-encoded when deployed to Azure app service Posted on September 11, 2019 at 8:35 PM by Stack Overflow RSS

For example the URL might be an endpoint that always returns JSON data. For more information see the documentation  Django community: Django Q&A RSS This page, updated regularly, aggregates Django Q&A from the Django community. Full JSON Isn't Receiving on Django REST Framework Registration Posted on December 2, 2019 at 6:12 PM by Stack Overflow RSS

Customizable all the way down - just use regular function-based views if you don'​t git clone Let's take a look at a quick example of using REST framework to build a simple model-backed API​. from django.conf.urls import url, include from django.contrib.auth.models  I add some data from public API to my sqlite database in And transmit it with django rest framework to axios (vuejs application display this data). Public API updated every second, i want run my code every time as well to update my database automaticly.

  • I doubt is very relevant for this question in particular, but python=3.7, django=2.1.2 and djangorestframework=3.8.2
  • The path simply stores "test", so the query SELECT * FROM filename WHERE path = 'C:\\test' fails, hence nothing is returned. For the database this is just a string, whether that string "happens" to be a filepath is irrelevant.
  • @WillemVanOnsem I'm of course creating the c:\test object. The second POST with the json data is in the question too :)
  • I tried, but it's not working neither. I don't think that's the issue as non-url-encoded files are found fine. By default the lookup_field should be the pk anyway.
  • By default the lookup_field is the primary key IIRC.
  • you can overide the get_object method
  • The get_object method is not even being called. The logs shows me Not Found: /filename/c:\test so django is decoding it properly.... maybe a URL path issue?
  • Hi babak, the JSON is working fine, the POST are successful. The issue is getting the object back via a GET request.
  • i think you can validate your return obj in your return call. try to use valid() in modelserializer.